2017-06-22 79 views
0

所以我做了一个Excel宏,当我尝试运行它时,它不断崩溃。它是:为什么我的Excel VBA宏崩溃?

result = "fail" 

j = 4 

Do While result = "fail" Or Cells(2, j) <> " " 

     If Cells(2, j).Value >= 15 Then 

      result = "pass" 


     Else 

      j = j + 1 

     End If 

Loop 
+1

“崩溃”是什么意思?它在什么情况下“崩溃”? –

+0

无限循环,因为增加j必须在两种情况下 –

回答

0

改变或与。 循环需要停止某个地方,目前它正在进入一个无限循环。 请尝试下面的代码,让我知道如果这是你想要的或我已经不正确地理解你的要求。

Sub tester() 


result = "fail" 

j = 4 

Do While result = "fail" And Cells(2, j) <> "" 
    If Cells(2, j).Value >= 15 Then 

     result = "pass" 


    Else 

     j = j + 1 

    End If 

Loop 
End Sub 
1

Do WhileDo While result = "fail" Or Cells(2, j) <> " "将运行如果任result = "fail"Cells(2, j) <> " "

我想你打算退出循环,一旦你到达一个空单元格,或者你得到result = "pass"。所以,你需要改变你的OrAnd

Do While result = "fail" And Cells(2, j) <> " " 

如果你想退出的情况下,只有在小区空的空间,也Trim补充。

Do While result = "fail" And Trim(Cells(2, j)) <> ""