为什么我的Excel VBA宏崩溃?
问题描述:
所以我做了一个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
答
改变或与。 循环需要停止某个地方,目前它正在进入一个无限循环。 请尝试下面的代码,让我知道如果这是你想要的或我已经不正确地理解你的要求。
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
答
你Do While
环Do While result = "fail" Or Cells(2, j) <> " "
将运行如果任result = "fail"
或Cells(2, j) <> " "
。
我想你打算退出循环,一旦你到达一个空单元格,或者你得到result = "pass"
。所以,你需要改变你的Or
到And
:
Do While result = "fail" And Cells(2, j) <> " "
如果你想退出的情况下,只有在小区空的空间,也Trim
补充。
Do While result = "fail" And Trim(Cells(2, j)) <> ""
“崩溃”是什么意思?它在什么情况下“崩溃”? –
无限循环,因为增加j必须在两种情况下 –