30 Advanced Excel Tips for 2026

Excel Champs / VBA / VBA IF AND (Test Multiple Conditions)
By Puneet Gogia (Microsoft MVP)

VBA IF AND (Test Multiple Conditions)

30+ Advanced Excel Tips for 2026

No spam. Only Excel Stuff. God Promise. 😊

Join conditions with And to run the code only when every one is True: If Range("B2").Value > 10000 And Range("C2").Value >= 20 Then gives the bonus when sales are over $10,000 and the person worked at least 20 days. If any condition is False, the Else part runs.

You can join as many conditions as you need. To mix in Or, use brackets to show which conditions go together: If (region = "East" Or region = "West") And sales > 10000 Then. Without brackets, VBA works out And first.

To test a range of values, write two conditions on the same variable: If score >= 60 And score < 80 Then. If 60 <= score < 80 Then gives no error but is always True, because VBA compares the first pair, gets True or False, then compares that result with 80.

VBA checks every condition, even after one is False. If Not rng Is Nothing And rng.Value > 0 Then still reads rng.Value when rng is Nothing and stops with error 91, so put the second test in its own If inside the first. A blank cell counts as 0 in a numeric test.

Let’s say you are managing a sales team. You want to give a bonus to salespeople who meet two conditions: they must have made sales over $10,000 and worked 20 days in the month. You can write a VBA code combining IF & AND to test two conditions.

You can use the AND operator with the VBA IF to test multiple conditions. When you use it, it allows you to test two conditions simultaneously and get true if both of those conditions are true. And if any of the conditions is false, it returns false in the result.

AND in VBA

In VBA, the AND operator checks if multiple conditions are true simultaneously. For example: If condition1 And condition2 Then. Both conditions must be true for the combined condition to be true. If any of the conditions is false, the combined condition is false.

Combining IF & AND

You can see in the below snapshot that you can use AND within the IF statement and specify two conditions to test. Now let’s write a code with this:

vba-if-statement
  1. First, start the IF statement with the “IF” keyword.
  2. After that, specify the first condition that you want to test.
  3. Next, use the AND keyword to specify the second condition.
  4. In the end, specify the second condition that you want to test.

To have a better understanding, let’s see an example.

Sub myMacro()
If 1 = 1 And 2 > 1 Then
    MsgBox "Both of the conditions are true."
Else
    MsgBox "One or both of the conditions are false."
End If
End Sub

If you look at the above example, we have specified two conditions: the first is (1 = 1) and the second is (2 > 1). Here, both of the conditions are true, and that’s why it executes the line of code that we have specified for a true result.

Now let’s see what happens if one of these two conditions is false. Let me use a different code here.

Sub myMacro1()
If 1 = 1 And 2 < 1 Then
    MsgBox "Both of the conditions are true."
Else
    MsgBox "One or both of the conditions are false."
End If
End Sub

This VBA code checks two conditions:

  • 1 = 1 (which is true)
  • 2 < 1 (which is false)

When you run this macro, since both conditions need to be true for the IF statement to return TRUE and here one is false, it runs the Else part code and shows the message “One or both of the conditions are false.”

Multiple Conditions with IF & AND

In the same way, you can also test more than two conditions at the same time. Let’s continue the above example and add a third condition to it.

Sub myMacro2()
If 1 = 1 And 2 > 1 And 1 - 1 = 0 Then
    MsgBox "All the conditions are true."
Else
    MsgBox "Some conditions are false."
End If
End Sub

Now we have three conditions to test, and we have used AND after the second condition to specify the third condition. As you learned above, when you use AND, all the conditions need to be true to get true in the result.

When you run this code, it executes the line of code that we have specified for true.

And if any of the conditions is false, just like you have in the following code, it returns false.

Sub myMacro3()
If 1 = 1 And 2 < 1 And 1 + 1 = 0 Then
    MsgBox "All the conditions are true."
Else
    MsgBox "Some conditions are false."
End If
End Sub

More Examples

Here are a few examples that show you how to write code in VBA using an IF statement and AND in combination.

This VBA code below checks if num1 is greater than 5 and num2 is less than 30. If both conditions are true, it shows “Both conditions are true.” Otherwise, it shows “One or both conditions are false.”

Sub myMacro4()
    Dim num1 As Integer
    Dim num2 As Integer
    
    num1 = 10
    num2 = 20
    
    If num1 > 5 And num2 < 30 Then
        MsgBox "Both conditions are true."
    Else
        MsgBox "One or both conditions are false."
    End If
End Sub

The VBA code below checks if the value in cell A1 is greater than 50 and the value in cell B1 is less than 100. If both conditions are true, it says, “Cell A1 is greater than 50 and Cell B1 is less than 100.” Otherwise, it shows “One or both conditions are false.”

Sub myMacro5()
    Dim cellA As Range
    Dim cellB As Range
    
    Set cellA = Range("A1")
    Set cellB = Range("B1")
    
    If cellA.Value > 50 And cellB.Value < 100 Then
        MsgBox "Cell A1 is greater than 50 and Cell B1 is less than 100."
    Else
        MsgBox "One or both conditions are false."
    End If
End Sub

The VBA code below checks if the text is “Hello” and the score is greater than 80. If both conditions are true, it shows a message box saying, “Text is ‘Hello’ and score is greater than 80.” Otherwise, it shows “One or both conditions are false.”

Sub myMacro6()
    Dim text As String
    Dim score As Integer
    
    text = "Hello"
    score = 85
    
    If text = "Hello" And score > 80 Then
        MsgBox "Text is 'Hello' and score is greater than 80."
    Else
        MsgBox "One or both conditions are false."
    End If
End Sub

The code below checks if the age is above 18. If true, it then checks if the score is between 80 and 100. Depending on these conditions, it shows appropriate messages about the age and score. If the age is 18 or below, it shows a message for that case.

Sub myMacro7()
    Dim age As Integer
    Dim score As Integer
    
    age = 22
    score = 90
    
    If age > 18 Then
        If score > 80 And score < 100 Then
            MsgBox "Age is above 18 and score is between 80 and 100."
        Else
            MsgBox "Age is above 18 but score is not in the desired range."
        End If
    Else
        MsgBox "Age is 18 or below."
    End If
End Sub

Example: Give a Bonus to Salespeople

Now let’s go back to the sales team example from the start. Let’s say you have the names in column A, the sales in column B and the days worked in column C, with headers in row 1. The code below checks each row and writes “Bonus” in column D only when the sales are more than $10,000 and the days worked are 20 or more:

Sub GiveBonus()
Dim lastRow As Long, i As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
If Cells(i, "B").Value > 10000 And Cells(i, "C").Value >= 20 Then
Cells(i, "D").Value = "Bonus"
Else
Cells(i, "D").Value = "No Bonus"
End If
Next i
End Sub

Combine AND with OR and NOT

You can use AND with OR and NOT in the same IF statement. VBA evaluates NOT first, then AND, and then OR. So always use parentheses to make the order clear. In the code below, the message shows when the region is “East” or “West”, and the status is not “Closed”:

Sub AndWithOrNot()
Dim region As String, status As String
region = "East"
status = "Open"
If (region = "East" Or region = "West") And Not status = "Closed" Then
MsgBox "Follow up with this client."
End If
End Sub

VBA AND Tests All the Conditions

VBA doesn’t stop at the first false condition when you use AND. It tests every condition, even when the first one is already false. So, if one of the conditions can give an error, the whole IF statement gives that error. In the code below, the second condition still runs when the value in A1 is text, and you get the “Type mismatch” error (Error 13):

Sub AndError()
If IsNumeric(Range("A1").Value) And Range("A1").Value * 2 > 100 Then
MsgBox "More than 50."
End If
End Sub

To avoid this, use a nested IF, so the second condition only runs when the first one is true:

Sub AndFixed()
If IsNumeric(Range("A1").Value) Then
If Range("A1").Value * 2 > 100 Then
MsgBox "More than 50."
End If
End If
End Sub

Frequently Asked Questions

Can I use AND in an ElseIf line?

Yes. You can use AND (and OR) in any ElseIf line, just like in the If line, for example ElseIf score > 50 And score <= 80 Then.

What is the difference between VBA AND and the Excel AND function?

In VBA, AND is an operator that you write between two conditions (condition1 And condition2). In a worksheet, AND is a function that takes the conditions as arguments, like the formula below. Both return TRUE only when all the conditions are true.

=AND(A1>50,B1<100)

How many conditions can I test with AND?

You can add as many conditions as you need, each one with its own AND. If the line gets too long, you can break it with a space and an underscore ( _ ) at the end of the line and carry on to the next line.

Puneet Gogia

Written by

Puneet Gogia

Microsoft MVP — Excel · Founder, Excel Champs

Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.

3M+ readers helped
1,000+ tutorials written
Since 2015 teaching Excel