VBA 做直到循环与或“”条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24633133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA Do Until Loop with Or "" condition
提问by KeithG
Do Until Sheets("Sheet1").Cells(x, y).Value = stringOne Or ""
x = x + 1
Loop
In the above VBA code slice, I get a Run-time error '13': Type mismatch. stringOne is declared a String. I can eliminate Or "" and the loop works. I can eliminate stringOne and only have "" and the loop works. Only when I add the Or "" does the loop stop functioning. I've even swapped order, i.e, = "" Or stringOne. Any thoughts?
在上面的 VBA 代码片段中,我收到一个运行时错误“13”:类型不匹配。stringOne 被声明为一个字符串。我可以消除 Or "" 并且循环起作用。我可以消除 stringOne 并且只有 "" 并且循环有效。只有当我添加 Or "" 时,循环才会停止运行。我什至交换了顺序,即 = "" 或 stringOne。有什么想法吗?
回答by Axel Amthor
Any condition needs expressions and operators.
任何条件都需要表达式和运算符。
Do until exp1 op exp2 OR exp3
might only be valid if exp3
is a boolean type. "" (String) is not.
可能只有在exp3
是布尔类型时才有效。"" (String) 不是。
So it should be like
所以它应该像
Do Until Sheets("Sheet1").Cells(x, y).Value = stringOne Or Sheets("Sheet1").Cells(x, y).Value = ""
回答by Allan S. Hansen
You need a boolean operation after the OR, so you basically need:
您需要在 OR 之后进行布尔运算,因此您基本上需要:
Do Until Sheets("Sheet1").Cells(x, y).Value = stringOne Or Sheets("Sheet1").Cells(x, y).Value = ""
x = x + 1
Loop
Or "" is not a boolean operation, and therefore you have
Do Until (boolean operation) OR (string constant)
instead of Do Until (boolean operation) OR (boolean operation)
或者 "" 不是布尔运算,因此你有
Do Until (boolean operation) OR (string constant)
而不是Do Until (boolean operation) OR (boolean operation)