-1

I've got the following code

Dim qt As QueryTable
For Each qt In ActiveSheet.QueryTables
    If qt.Sql = SQLSTRING Then Exit For
Next qt
If qt Is Nothing Or qt.Sql <> SQLSTRING Then 
     DoStuff 
End If

I have a question because I can see that 'qt is nothing' is evaluating to true, but on the same line, I get a '91: Object variable or With block variable not set' error. It's not an issue as I can easily catch the error, but tbh I was surprised, as I thought if the first clause of an 'or' evaluates to true, it shouldn't even try to evaluate the second clause...is this not something I can rely on in vba?

GKen
  • 41
  • 1
  • 6

1 Answers1

0

VBA is not so clever, use:

If qt Is Nothing Then
    DoStuff
Else
    If qt.Sql <> SQLSTRING Then
        DoStuff
    End If
End If
Gary's Student
  • 95,722
  • 10
  • 59
  • 99