VBA (Visual Basic for Applications) code in Excel or Word can be temporarily disabled to test changes, debug errors, or isolate issues without losing the original code. Commenting out code achieves this by marking sections as "read-only" or inactive, preserving all your work while halting execution. This is essential during development, troubleshooting, or when you want to pause a macro midway for manual testing. All you need is your VBA editor open—no additional tools are required.

What You'll Need

  • A Microsoft Office application (Excel, Word, Access) with VBA enabled.
  • Your project workbook (.xlsm or .docm) open in the correct module.
  • Basic familiarity with VBA syntax (optional but helpful).

Step-by-Step

  1. Open the VBA Editor:
    1. Press Alt+F11 to launch the VBA editor for Excel (or Alt+F11 for Word).
    2. If prompted to load a module, click Yes to the workbook where your code resides.

    If you’re working in a module (e.g., Module1 or ThisWorkbook), the editor will show its contents. For classes or user-defined types, you may need to double-click the component to open it first.

  2. Find the code block you want to disable:
    1. Use the Find feature (Ctrl+F) to search for specific keywords (e.g., event procedures like Worksheet_Change or lines like Range("A1").Value = 10).
    2. If no search term matches, scroll through the code visually or use the Go To dialog (Ctrl+G) to jump to lines (e.g., enter 100 to skip to line 100).
  3. Comment out the code:
    1. Select the lines you want to disable by clicking and dragging (or holding Shift to add lines).
    2. Add the Rem (short for "remark") prefix to each line. VBA treats everything after Rem as a comment until the end of the line: Rem This line is ignored by VBA Rem Another ignored line
      1. For single-line comments, add Rem at the beginning of each line.
      2. For multiple lines, you can also use the '* symbol (though Rem is more readable). Example: * This section is disabled * Code below will not run
  4. Test the commented-out code:
    1. Run your macro or trigger the event again (e.g., click a button, run Sub RunExample() via F5).
    2. Verify the commented lines no longer execute. If they do, check for typos in the Rem prefix or syntax errors elsewhere in the code.
  5. Uncomment the code later:
    1. Return to the VBA editor and remove the Rem prefix from the selected lines.
    2. Ensure the code runs as expected or debug further if needed.

Common Mistakes

Mistakes commonly occur when overcomplicating comments or misapplying syntax. Avoid these pitfalls:

  1. Not preserving indentation:

    VBA expects consistent indentation for readability. Remove comments from your code’s formatting—only the Rem prefix matters. Indentation helps you track changes but doesn’t affect execution.

  2. Using block comments incorrectly:

    While VBA supports multi-line comments with '*, it’s less common and more error-prone than Rem. Example of a block comment: '* Removed code block: '* Dim x As Integer '* x = 10 '*' End comment block (requires closing '*' on the last line)

    Block comments can cause issues if you accidentally omit the closing * or mix them with Rem.

  3. Forgetting to test after commenting:

    Always run your macro or trigger the event again to confirm the code is disabled. Some macros rely on hidden variables or conditions that might still execute.

  4. Commenting out event procedures incorrectly:

    For event-driven macros (e.g., Worksheet_Change), comment out the entire procedure block, not just individual lines. Example: Private Sub Worksheet_Change(ByVal Target As Range) Rem If Target.Address = "$A$1", MsgBox "Changed!" ' ' End of commented-out code

    Skipping this can leave parts of the event handler active despite the rest being commented out.

Tips

To work smarter when commenting out VBA code:

  1. Use keyboard shortcuts:

    For repetitive commenting, assign a shortcut in VBA:

    1. Go to Tools > Options > Environment Settings > Customize... in the VBA editor.
    2. Add a macro to the Macros list that runs: Selection.AddComment "/*" & Selection.Start & "-" & Selection.End & "*/" or use the Rem method programmatically via a button macro.

  2. Backup your code before editing:

    Save a copy of your workbook as a .xlsm (Excel) or .docm (Word) before making changes. Use the File > Save As option to create a backup file.

  3. Use comments to document changes:

    Instead of just commenting out code, add descriptive comments above the section to explain why it’s disabled. Example: Rem ============================================= Rem [DEVELOPMENT] Disable auto-save during testing Rem ============================================= Application.SaveAutoRecoveryInterval = 0

  4. For large projects, organize comments:

    Add a header comment at the top of each module to note what’s disabled and why. Example: Module Module2 Rem ============================================= Rem DISABLED: Error handling for Worksheet_Activate Rem =============================================

Troubleshooting

If commenting out code doesn’t work as expected, check these common issues:

  1. The code still runs:
    1. Verify the Rem prefix is on every line. Some lines might have extra spaces or hidden characters.
    2. Check for syntax errors in the commented-out code—VBA may skip to the next line if it encounters an error.
    3. Test with a single-line comment first (removing Rem from one line) to isolate the issue.
  2. Event procedures don’t respond:
    1. Ensure the event name matches exactly (e.g., Worksheet_Change, not worksheet_change).
    2. Check the module where the event is declared is open in the VBA editor.
    3. Run the macro via the IDE (F5) instead of manually triggering the event.
  3. Macros don’t trigger at all:
    1. Restart Excel/Word to clear any cached events.
    2. Reassign buttons or triggers to ensure they’re pointing to the correct macro.
    3. Check for macro security warnings in File > Options > Trust Center > Macro Settings.
  4. Comments appear in output (e.g., messages):
    1. Some VBA functions return comments as part of error messages. Check for lines like MsgBox "This is a comment" and comment those out instead of the entire procedure.
    2. Use On Error Resume Next temporarily to suppress error messages (but avoid this in production code).

Frequently Asked Questions

How do I comment out a whole macro at once?

To comment out an entire macro, wrap the entire procedure in a single Rem line at the top. Example: Rem Sub DisableAllMacros() Rem ' Code below will not run Rem End Sub However, this is less practical than commenting line-by-line for fine-grained control.

Can I comment out code in a class module?

Yes, but use Rem or the * symbol at the class level or within methods. For example: Class Module MyClass Rem Sub DisableMethod() Rem ' Code here is ignored Rem End Sub

What happens if I comment out a line with a variable declaration?

Variable declarations (e.g., Dim x As Integer) must still be commented out properly. If you don’t, VBA may still try to use the variable name, leading to errors. Example: Rem Dim x As Integer ' Correct x = 10 ' Compile error if x is never declared

How do I temporarily comment out a module entirely?

Add a comment to the module’s name or prefix it with Rem. Example: Rem Module1 ' Disables all code in Module1 Or rename the module to start with Rem_ (though this isn’t a standard VBA comment method).

Can I use different comment styles for different languages?

VBA’s comment syntax is language-agnostic. Use Rem for clarity, but * works too. For multi-language projects, stick to Rem as it’s more consistent.

Why won’t my commented-out code run even after refreshing?

Refreshing the VBA editor (Ctrl+R) won’t re-execute commented-out code. The only way to "uncomment" it is to remove the Rem prefix or use a workaround like assigning a macro to a button that skips the commented section.