Kei*_*thG 4 excel vba loops excel-vba conditional-statements
Do Until Sheets("Sheet1").Cells(x, y).Value = stringOne Or ""
x = x + 1
Loop
Run Code Online (Sandbox Code Playgroud)
在上面的VBA代码片中,我得到运行时错误"13":类型不匹配.stringOne声明为String.我可以消除或""和循环工作.我可以消除stringOne,只有""并且循环工作.只有当我添加Or""时,循环才会停止运行.我甚至交换了订单,即=""或stringOne.有什么想法吗?
Axe*_*hor 10
任何条件都需要表达式和运算符
Do until exp1 op exp2 OR exp3
Run Code Online (Sandbox Code Playgroud)
如果exp3是布尔类型,则可能仅有效.""(字符串)不是.
所以应该是这样的
Do Until Sheets("Sheet1").Cells(x, y).Value = stringOne Or Sheets("Sheet1").Cells(x, y).Value = ""
Run Code Online (Sandbox Code Playgroud)
在OR之后需要一个布尔运算,所以你基本上需要:
Do Until Sheets("Sheet1").Cells(x, y).Value = stringOne Or Sheets("Sheet1").Cells(x, y).Value = ""
x = x + 1
Loop
Run Code Online (Sandbox Code Playgroud)
或者""不是布尔操作,因此您可以
Do Until (boolean operation) OR (string constant)代替Do Until (boolean operation) OR (boolean operation)