2017-08-07 158 views
-2

我的代碼有91錯誤,似乎沒有任何工作。在VBA Excel中保持91'錯誤

下一個代碼是根據另一個表來標識包含LOW,MEDIUM,HIGH的每一行,問題是我的數據在時間不包含'LOW',我想我需要添加一個IF代碼可以開始搜索MEDIUM值,但我不知道該怎麼做。


Private Sub CommandButton1_Click() 

Dim DataCalc_LOW As Integer 
Dim DataCalc_MEDIUM As Integer 
Dim DataCalc_HIGH As Integer 

DataToCalc_LOW = Worksheets("Random Generator").Range("AL16").Value 
DataToCalc_MEDIUM = Worksheets("Random Generator").Range("AL17").Value 
DataToCalc_HIGH = Worksheets("Random Generator").Range("AL18").Value 

Dim x As Integer 
Dim y As Integer 

Range("I14").Select 
Range(Selection, Selection.End(xlDown)).Select 

'x = 0 

Do 
x = x + 1 
Selection.Find(What:="LOW", After:=ActiveCell, LookIn:=xlFormulas, LookAt _ 
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False,     
SearchFormat:=False).Activate 
With ActiveCell 
.Interior.ColorIndex = 44 
End With 
ActiveCell.Select 
Selection.Offset(0, 0).Select 
Range(Selection, Selection.End(xlDown)).Select 

Loop Until x = DataToCalc_LOW 

Range("I14").Select 
Range(Selection, Selection.End(xlDown)).Select 

'y = 0 

Do 
y = y + 1 
Selection.Find(What:="MEDIUM", After:=ActiveCell, LookIn:=xlFormulas, LookAt _ 
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False,     
SearchFormat:=False).Activate 
With ActiveCell 
.Interior.ColorIndex = 44 
End With 
ActiveCell.Select 
Selection.Offset(0, 0).Select 
Range(Selection, Selection.End(xlDown)).Select 

Loop Until y = DataToCalc_MEDIUM 

Range("I14").Select 
Range(Selection, Selection.End(xlDown)).Select 

'Z = 0 
Do 
Z = Z + 1 
Selection.Find(What:="HIGH", After:=ActiveCell, LookIn:=xlFormulas, LookAt _ 
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False,   
SearchFormat:=False).Activate 
With ActiveCell 
.Interior.ColorIndex = 44 
End With 
ActiveCell.Select 
Selection.Offset(0, 0).Select 
Range(Selection, Selection.End(xlDown)).Select 

Loop Until Z = DataToCalc_HIGH 

For i = 15 To 5000 
cuenta = WorksheetFunction.CountIf(Range("F15:F500"), Cells(i, "F")) 
If cuenta = 1 Then 
    Cells(i, "I").Interior.ColorIndex = 44 
End If 
Next 
'ActiveSheet.Protect Password:="QC" 

End Sub 
+3

請爲了您的利益,請試圖幫助本網站的人,以及那些將繼承該代碼的人,瞭解如何縮進。或者[使用一個工具爲你做](http://rubberduckvba.com/indentation)。 –

回答

0

你的循環Do...Loop Until (condition)至少運行一次,並且條件在循環的末尾被選中。您可以改用Do Until (condition)...Loop循環。在這種情況下,在開始時檢查條件,如果它已經是真的,那麼循環將不會運行。