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
- Open the VBA Editor:
- Press Alt+F11 to launch the VBA editor for Excel (or Alt+F11 for Word).
- If prompted to load a module, click Yes to the workbook where your code resides.
If you’re working in a module (e.g.,
Module1orThisWorkbook), the editor will show its contents. For classes or user-defined types, you may need to double-click the component to open it first. - Find the code block you want to disable:
- Use the
Findfeature (Ctrl+F) to search for specific keywords (e.g., event procedures likeWorksheet_Changeor lines likeRange("A1").Value = 10). - If no search term matches, scroll through the code visually or use the
Go Todialog (Ctrl+G) to jump to lines (e.g., enter100to skip to line 100).
- Use the
- Comment out the code:
- Select the lines you want to disable by clicking and dragging (or holding Shift to add lines).
- Add the
Rem(short for "remark") prefix to each line. VBA treats everything afterRemas a comment until the end of the line:Rem This line is ignored by VBARem Another ignored line - For single-line comments, add
Remat the beginning of each line. - For multiple lines, you can also use the
'*symbol (thoughRemis more readable). Example:* This section is disabled * Code below will not run
- Test the commented-out code:
- Run your macro or trigger the event again (e.g., click a button, run
Sub RunExample()via F5). - Verify the commented lines no longer execute. If they do, check for typos in the
Remprefix or syntax errors elsewhere in the code.
- Run your macro or trigger the event again (e.g., click a button, run
- Uncomment the code later:
- Return to the VBA editor and remove the
Remprefix from the selected lines. - Ensure the code runs as expected or debug further if needed.
- Return to the VBA editor and remove the
Common Mistakes
Mistakes commonly occur when overcomplicating comments or misapplying syntax. Avoid these pitfalls:
- Not preserving indentation:
VBA expects consistent indentation for readability. Remove comments from your code’s formatting—only the
Remprefix matters. Indentation helps you track changes but doesn’t affect execution. - Using block comments incorrectly:
While VBA supports multi-line comments with
'*, it’s less common and more error-prone thanRem. 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 withRem. - 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.
- 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 codeSkipping this can leave parts of the event handler active despite the rest being commented out.
Tips
To work smarter when commenting out VBA code:
- Use keyboard shortcuts:
For repetitive commenting, assign a shortcut in VBA:
- Go to
Tools > Options > Environment Settings > Customize...in the VBA editor. - Add a macro to the
Macroslist that runs:Selection.AddComment "/*" & Selection.Start & "-" & Selection.End & "*/"or use theRemmethod programmatically via a button macro.
- Go to
- Backup your code before editing:
Save a copy of your workbook as a
.xlsm(Excel) or.docm(Word) before making changes. Use theFile > Save Asoption to create a backup file. - 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 - 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:
- The code still runs:
- Verify the
Remprefix is on every line. Some lines might have extra spaces or hidden characters. - Check for syntax errors in the commented-out code—VBA may skip to the next line if it encounters an error.
- Test with a single-line comment first (removing
Remfrom one line) to isolate the issue.
- Verify the
- Event procedures don’t respond:
- Ensure the event name matches exactly (e.g.,
Worksheet_Change, notworksheet_change). - Check the module where the event is declared is open in the VBA editor.
- Run the macro via the IDE (F5) instead of manually triggering the event.
- Ensure the event name matches exactly (e.g.,
- Macros don’t trigger at all:
- Restart Excel/Word to clear any cached events.
- Reassign buttons or triggers to ensure they’re pointing to the correct macro.
- Check for macro security warnings in
File > Options > Trust Center > Macro Settings.
- Comments appear in output (e.g., messages):
- 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. - Use
On Error Resume Nexttemporarily to suppress error messages (but avoid this in production code).
- Some VBA functions return comments as part of error messages. Check for lines like
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.