2015-08-20 5 views
7

Zastanawiam się, jak działa instrukcja warunkowa Or w VBA/VB6. Zasadniczo, jeśli mamy If A Or B Then, w jakiej kolejności są oceniane wyrażenia Boolean? Jeśli A jest prawdziwe, czy B jest również oceniany?W jaki sposób ocena Orzeczu jest oceniana w VBA?

+0

oba zostaną ocenione niezależnie. W VBA uważam, że jedynym sposobem jest podzielenie ich na oddzielne zagnieżdżone instrukcje if. –

+0

https://msdn.microsoft.com/PL/library/office/gg264205.aspx – findwindow

+0

Poprzednio: http://stackoverflow.com/questions/24641923/vba-short-circuit-and-alternatives, http://stackoverflow.com/questions/3242560/andalso-orelse-in-vba/3245183#3245183 –

Odpowiedz

5

Oto niektóre wyniki badań dla Ciebie:

Public Sub DoTesting() 

    ' Displays "Test1" followed by "Test2", so they're evaluated in order. 
    ' Also, both expressions are evaluated even though Test1() is False. 
    If Test1() And Test2() Then 
    End If 

    ' Displays "Test2" followed by "Test1", so they're evaluated in order. 
    ' Also, both expressions are evaluated even though Test2() is True. 
    If Test2() Or Test1() Then 
    End If 

    ' Displays "Test1" only. Test2() is not evaluated. 
    If Test1() Then If Test2() Then Debug.Print "" 

End Sub 

Public Function Test1() As Boolean 
    MsgBox "Test1" 
    Test1 = False 
End Function 

Public Function Test2() As Boolean 
    MsgBox "Test2" 
    Test2 = True 
End Function 

Zatem oba wyrażenia w Or lub And zawsze są oceniane w kolejności, niezależnie od wyniku. Możesz użyć If ... Then If ... Then, aby uzyskać proste wbudowane zwarcie.

Powiązane problemy