Does the VBA “And” operator evaluate the second argument when the first is false ?

Does the VBA “And” operator evaluate the second argument when the first is false ?

Asked on January 10, 2019 in Excel.
Add Comment


  • 3 Answer(s)

    Try this best solution :

    VBA “And” operator evaluate the second argument when the first is false is called “short-circuit evaluation”.

    But VBA have not it.

      This way that was chosen there complicated alternative a Select Case for the If. And this is also an example of using nested Ifs.

    Answered on January 10, 2019.
    Add Comment

     VBA does not have short-circuit evaluation.

         Alternatively using the AND operator, because It is technically able to use 2 If-then statements. If doing it a lot of times for unnecessary, so did not notice the savings, so go for whatever is more readable. VBA handles multiple If-then statements faster than Select Case, if need to have really technical, 

    Answered on January 10, 2019.
    Add Comment

    It create a big anomaly in a situation like this:

    If i <= UBound(Arr, 1) And j <= UBound(Arr, 2) And Arr(i, 1) <= UBound(Arr2, 1) Then
       Arr2(Arr(i, 1), j) = Arr(i, j)
    End If
    
       which is wrong, more suitably :

    If i <= UBound(Arr, 1) And j <= UBound(Arr, 2) Then
      If Arr(i, 1) <= UBound(Arr2, 1) Then
        Arr2(Arr(i, 1), j) = Arr(i, j)
      End If
    End If
    
    In case having dislike to nested ifs:

    If i > UBound(Arr, 1) Or j > UBound(Arr, 2) Then
       *// Do Nothing
    ElseIf Arr(i, 1) > UBound(Arr2, 1) Then
       *// Do Nothing
    Else
       Arr2(Arr(i, 1), j) = Arr(i, j)
    End If
    
    Answered on January 10, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.