Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 371
KeithG Avatar asked Dec 07 '22 01:12

KeithG


2 Answers

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 = ""
like image 70
Axel Amthor Avatar answered Dec 20 '22 15:12

Axel Amthor


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)

like image 33
Allan S. Hansen Avatar answered Dec 20 '22 16:12

Allan S. Hansen