当前位置: 动力学知识库 > 问答 > 编程问答 >

excel vba - VBA Do Until Loop with Or "" condition

问题描述:

 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?

网友答案:

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.

So it should be like

 Do Until Sheets("Sheet1").Cells(x, y).Value = stringOne Or Sheets("Sheet1").Cells(x, y).Value = ""
网友答案:

You need a boolean operation after the OR, so you basically need:

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)

分享给朋友:
您可能感兴趣的文章:
随机阅读: