Using Excel VBA with an If True statement allows you to execute code only when a condition is met, enabling automation based on real-time data or user input. It doesn't mean the condition is literally true—it means you're evaluating whether a specified expression evaluates to True, and if so, running a block of code. This is essential for building intelligent macros that respond to cell values, user actions, or system states. No special tools are required beyond Microsoft Excel with VBA enabled.

What You'll Need

  • Microsoft Excel 2010 or later (Windows or Mac)
  • Enable the Developer tab in Excel (if not already visible)
  • Basic understanding of Excel cells, ranges, and worksheet structure
  • Access to the Visual Basic for Applications editor (press Alt + F11)

Step-by-Step

  1. Open Excel and create a new blank workbook. Save it as a macro-enabled file (.xlsm) by selecting File > Save As, then choosing "Excel Macro-Enabled Workbook (*.xlsm)" from the file type dropdown.
  2. Press Alt + F11 to open the Visual Basic for Applications (VBA) editor. If the Project Explorer is not visible, press Ctrl + R to display it.
  3. In the Project Explorer, right-click on the workbook name (e.g., VBAProject (YourWorkbook.xlsm)), then select Insert > Module. This creates a new code module where you'll write your logic.
  4. Type or paste the following basic If True structure into the module:
    Sub RunIfTrue()
        If True Then
            MsgBox "This code runs because the condition is True"
        End If
    End Sub
    This example always runs the MsgBox because True is a literal Boolean value.
  5. Replace the literal True with a dynamic condition. For example, check if cell A1 contains the text "Complete":
    Sub CheckCellStatus()
        If Range("A1").Value = "Complete" Then
            Range("B1").Value = "Approved"
            Range("B1").Interior.Color = RGB(0, 255, 0)
        End If
    End Sub
    This code only executes if A1 exactly matches "Complete" (case-sensitive).
  6. Save your module by clicking the disk icon in the VBA editor or pressing Ctrl + S.
  7. Return to Excel by pressing Alt + Q. Go to the Developer tab, click Macros, select "CheckCellStatus", then click Run.
  8. Test the macro by typing "Complete" into cell A1, then running the macro again. You should see "Approved" appear in B1 with a green background.
  9. For multiple conditions, use ElseIf or Else to handle alternate outcomes:
    Sub MultiConditionCheck()
        Select Case Range("A1").Value
            Case "Complete"
                Range("B1").Value = "Approved"
                Range("B1").Interior.Color = RGB(0, 255, 0)
            Case "Pending"
                Range("B1").Value = "Review Required"
                Range("B1").Interior.Color = RGB(255, 255, 0)
            Case Else
                Range("B1").Value = "Unknown Status"
                Range("B1").Interior.Color = RGB(255, 0, 0)
        End Select
    End Sub
    This version handles three distinct states cleanly.
  10. To run the macro automatically when a cell changes, use the worksheet’s Change event. Double-click the worksheet name (e.g., Sheet1) in the Project Explorer, then paste:
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("A1")) Is Nothing Then
            If Range("A1").Value = "Complete" Then
                Range("B1").Value = "Approved"
                Range("B1").Interior.Color = RGB(0, 255, 0)
            End If
        End If
    End Sub
    This triggers the logic instantly when A1 is modified.

Common Mistakes

  • Using = instead of ==: VBA uses a single = for assignment and comparison. Writing If Range("A1").Value == "Complete" will cause a syntax error.
  • Forgetting End If: Every If block must end with End If. Missing this causes compilation errors when you try to run the macro.
  • Case sensitivity: "complete" is not the same as "Complete". Use UCase() or LCase() if you need to ignore case: If UCase(Range("A1").Value) = "COMPLETE" Then.
  • Referencing empty cells: If A1 is blank, Range("A1").Value returns an empty string, not Null. Use IsEmpty() if you need to detect truly blank cells.
  • Running the code in the wrong module: Code must be in a standard module for Sub procedures, or in a worksheet module for event-driven triggers like Worksheet_Change.

Tips

  • Use constants for repeated values: Define Const STATUS_COMPLETE As String = "Complete" at the top of your module to avoid typos and make updates easier.
  • Comment your logic: Add ' comments to explain what each condition checks. This helps future you or colleagues understand the intent.
  • Test with breakpoints: Press F9 on a line to set a breakpoint (red dot). Run the macro and it will pause there—inspect variables in the Locals window.
  • Use Debug.Print to log values to the Immediate Window (Ctrl + G in VBA editor), useful for tracking what a cell actually contains.
  • Wrap complex conditions in parentheses: If (Range("A1").Value = "Complete") And (Range("C1").Value > 0) Then improves readability and ensures correct evaluation order.
  • Always test on a copy of your data. Macros that modify cells can’t be undone with Ctrl + Z.

Troubleshooting

  • If the macro doesn’t run, check that macros are enabled. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select "Enable all macros" temporarily for testing.
  • If you see a runtime error like "Object required", you likely referenced a worksheet or range that doesn’t exist. Double-check the sheet name and cell reference spelling.
  • If the code runs but does nothing, use Debug.Print to output the value being tested. For example, add Debug.Print Range("A1").Value to see exactly what the macro is reading.
  • If the macro runs but triggers unexpectedly, check for multiple event triggers. Avoid placing Change events that modify cells within the same range being watched—this can cause loops.
  • If the VBA editor won’t open, ensure the Developer tab is enabled: File > Options > Customize Ribbon > check "Developer" under Main Tabs.
  • If you get a "Compile error: Expected: End If", scan your code for missing End If statements. Use the VBA editor’s auto-indentation (Ctrl + I) to spot unmatched blocks.

Frequently Asked Questions

Can I use If True with multiple conditions?

Yes, combine conditions with And, Or, or Not. Example: If Range("A1").Value = "Complete" And Range("B1").Value > 10 Then.

Does If True always run, or is it just an example?

If True always runs because it’s a literal Boolean value. You’ll typically replace True with a variable or expression like Range("A1").Value = "Yes".

Can I use If True in Excel formulas?

No. Excel formulas use IF(), not VBA’s If statement. VBA only works in macros, not in worksheet cells.

Why does my macro run but change nothing?

Most likely, your condition isn’t being met. Check the exact value in the cell—spaces, extra characters, or data types (text vs number) can cause mismatches.

What if I want to run code when a condition is False?

Use Else: If condition Then ... Else ... End If. The Else block runs only when the condition evaluates to False.

Can I nest If statements inside other If statements?

Yes. Nesting is common and valid. Just ensure each If has a matching End If. Use indentation to keep your code readable.