VBA Comments: Complete Guide with Best Practices (2025)

Updated: January 20257 min read

Quick Answer: Add comments in VBA using an apostrophe ('). Everything after ' on a line becomes a comment. Example: ' This is a comment. Comments help document your code and are ignored during execution.

What are VBA Comments?

Comments are non-executable lines of text in your VBA code that explain what the code does. VBA ignores comments completely during execution, so they exist purely for human readers to understand the code.

Simple Comment Example:

Sub CalculateTotal()
    ' This macro calculates the total sales
    Dim total As Double
    
    ' Loop through all rows
    For i = 2 To 100
        total = total + Range("C" & i).Value  ' Add each sale
    Next i
    
    ' Display result
    MsgBox "Total: $" & total
End Sub

Comments appear in green in the VBA Editor.

Why Use Comments?

  • Explain complex logic: Help others (and future you) understand why code works a certain way
  • Document assumptions: Note business rules or requirements
  • Temporarily disable code: Comment out lines for testing without deleting them
  • Add TODO reminders: Mark areas that need future work
  • Improve collaboration: Help team members understand your code

Comment Syntax

Apostrophe (') - Most Common

Use an apostrophe to start a comment. Everything after ' is ignored.

' This is a comment
Dim x As Integer  ' This is also a comment
x = 10  ' Set x to 10

Rem Keyword - Less Common

Use Rem keyword. Must be at start of line or after colon.

Rem This is a comment
x = 10: Rem This works
' x = 10 Rem This doesn't work (use apostrophe instead)

Block Comments

VBA has no multi-line syntax. Add apostrophe to each line or use Comment Block button.

' This is a multi-line comment
' that spans several lines
' Each line needs an apostrophe
' at the beginning

Keyboard Shortcuts:

Comment Block:

1. Select multiple lines in VBA Editor

2. Click the Comment Block button (toolbar)

3. Or use Edit → Comment Block

Uncomment Block:

1. Select commented lines

2. Click the Uncomment Block button (toolbar)

3. Or use Edit → Uncomment Block

Comment Best Practices

✓ DO: Explain WHY, Not WHAT

' BAD: Increment x by 1
x = x + 1

' GOOD: Add 1 to account for header row
x = x + 1

Good code should be self-explanatory. Comments should explain the reasoning behind decisions.

✓ DO: Document Complex Logic

' Calculate weighted average using 70% current + 30% previous
' This formula matches the business requirement from Q3 2024
result = (current * 0.7) + (previous * 0.3)

✓ DO: Add TODO Comments

' TODO: Add error handling for missing files
' TODO: Optimize this loop for large datasets
' FIXME: This breaks when column count exceeds 100

✗ DON'T: State the Obvious

' BAD: Declare variable x as integer
Dim x As Integer

' BAD: Set x to 10
x = 10

These comments add no value. The code already says what it does.

✗ DON'T: Leave Commented-Out Code

' x = oldCalculation()  ' Old method
' y = anotherOldMethod()  ' Deprecated
x = newCalculation()  ' Current method

Delete old code instead of commenting it out. Use version control to track history.

💡 TIP: Use Descriptive Variable Names

' Instead of this:
Dim x As Integer  ' Total sales

' Do this:
Dim totalSales As Integer  ' No comment needed!

Good variable names reduce the need for comments.

Practical Comment Examples

Example 1: Header Comments

'*****************************************************
' Macro: GenerateMonthlyReport
' Purpose: Creates formatted monthly sales report
' Author: John Smith
' Date: 2025-01-15
' Modified: 2025-01-18 - Added chart generation
'*****************************************************
Sub GenerateMonthlyReport()
    ' Code here
End Sub

Example 2: Inline Comments

Sub ProcessData()
    Dim lastRow As Long
    Dim i As Long
    
    lastRow = Cells(Rows.Count, 1).End(xlUp).Row  ' Find last row with data
    
    For i = 2 To lastRow  ' Start at 2 to skip header
        If Range("A" & i).Value > 1000 Then  ' Flag high-value items
            Range("D" & i).Value = "High"
        End If
    Next i
End Sub

Example 3: Debugging Comments

Sub TestMacro()
    Dim result As Double
    
    ' Temporarily disabled - testing new calculation method
    ' result = OldCalculation()
    
    result = NewCalculation()
    
    ' Debug: Display intermediate value
    ' MsgBox "Result: " & result
    
    Range("A1").Value = result
End Sub

Example 4: Business Logic Comments

Sub CalculateDiscount()
    Dim orderTotal As Double
    Dim discount As Double
    
    orderTotal = Range("B2").Value
    
    ' Business rule: 10% discount for orders over $500
    ' 15% discount for orders over $1000
    ' As per Sales Policy 2025-Q1
    If orderTotal > 1000 Then
        discount = orderTotal * 0.15
    ElseIf orderTotal > 500 Then
        discount = orderTotal * 0.1
    Else
        discount = 0
    End If
    
    Range("C2").Value = discount
End Sub

Frequently Asked Questions

How do I add a comment in VBA?

Use an apostrophe (') at the beginning of a line or after code. Everything after the apostrophe becomes a comment. Example: ' This is a comment. You can also use Rem keyword, but apostrophe is more common.

What is the keyboard shortcut for commenting in VBA?

In the VBA Editor, select lines and click the Comment Block button in the toolbar, or use Ctrl+M (if configured). To uncomment, use the Uncomment Block button or Ctrl+Shift+M.

Can I create multi-line comments in VBA?

VBA doesn't have true multi-line comment syntax like /* */. Instead, add an apostrophe at the start of each line you want to comment, or select multiple lines and use the Comment Block button in the VBA Editor.

What's the difference between ' and Rem in VBA?

Both create comments, but apostrophe (') is preferred because it's shorter and can be placed after code on the same line. Rem must be at the start of a line or after a colon. Example: x = 5 ' This works vs x = 5 Rem This doesn't work.

Do comments slow down VBA code?

No, comments have zero impact on performance. VBA ignores all comments during execution, so you can add as many as needed without affecting speed or memory usage.

Should I comment every line of VBA code?

No, comment only when necessary. Explain why code does something, not what it does (good code should be self-explanatory). Comment complex logic, business rules, workarounds, and non-obvious decisions.

Can I use comments to temporarily disable code?

Yes, commenting out code is a common debugging technique. Add apostrophes to disable lines temporarily without deleting them. This helps test different approaches or isolate bugs.

What color are comments in the VBA Editor?

Comments appear in green by default in the VBA Editor. This color coding helps distinguish comments from executable code. You can change this color in Tools → Options → Editor Format.

Need Help Writing VBA Code?

Our AI-powered VBA Code Generator creates working VBA code from plain English descriptions. No coding knowledge required!

✓ No credit card required ✓ 5 free generations ✓ Instant results

Related Articles