Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does AND function work in VBA?

Tags:

excel

vba

I'm sorry if this is a stupid question, but does the (Excel VBA) AND function checks EVERY single condition in it then proceed, or stops at the first FALSE condition and not checks the other? I'd like to know for optimization purposes and found nothing about it on the web so far.. Thanks in advance!

Example: If InStr(txt, "abc") > 0 And InStr(txt, "xyz") > 0 Then

so if "abc" not found in the txt variable (gives 'false'), then will the macro check the "xyz" too or skip the rest conditions


1 Answers

It appears that VBA keeps on going:

Sub ANDTest()
    If f1() And f2() Then MsgBox "X"
End Sub

Public Function f1() As Boolean
    f1 = False
End Function

Public Function f2() As Boolean
    MsgBox "in f2"
    f2 = False
End Function

enter image description here

like image 91
Gary's Student Avatar answered Sep 05 '26 06:09

Gary's Student