VBA IF Statement - A Complete Guide - Excel Macro Mastery

This post provides a complete guide to the VBA If Statement in VBA. If you are looking for the syntax then check out the quick guide in the first section which includes some examples.

The table of contents below provides an overview of what is included in the post. You use this to navigate to the section you want or you can read the post from start to finish.

“Guess, if you can, and choose, if you dare.” – Pierre Corneille

Contents

  • 1 Quick Guide to the VBA If Statement
  • 2 What is the VBA If Statement
  • 3 The Test Data and Source Code
  • 4 Format of the VBA If-Then Statement
    • 4.1 Indenting Between If and End If
  • 5 A Simple If Then Example
  • 6 Using Conditions with the VBA If Statement
  • 7 Using ElseIf with the VBA If Statement
  • 8 Using Else With the VBA If Statement
  • 9 Using Logical Operators with the VBA If Statement
    • 9.1 Using If And
    • 9.2 Using If Or
    • 9.3 Using If Not
  • 10 The IIF function
    • 10.1 Using Nested IIf
    • 10.2 What to Watch Out For
    • 10.3 If Versus IIf
  • 11 Using Select Case
    • 11.1 Using Case Is
  • 12 What’s Next?

Quick Guide to the VBA If Statement

DescriptionFormatExample
If ThenIf [condition is true] Then [do something] End IfIf score = 100 Then Debug.Print "Perfect" End If
If Else If [condition is true] Then [do something] Else [do something] End IfIf score = 100 Then Debug.Print "Perfect" Else Debug.Print "Try again" End If
If ElseIf If [condition 1 is true] Then [do something] ElseIf [condition 2 is true] Then [do something] End IfIf score = 100 Then Debug.Print "Perfect" ElseIf score > 50 Then Debug.Print "Passed" ElseIf score