Excel VBA has no built-in block comment button, but you can comment out a block of code in seconds by selecting the lines and using View > Toolbars > Edit, then clicking the Comment Block button. The manual alternative is to put a single apostrophe at the start of each line (or type ' before the code), and the fastest repeatable option is to build a tiny helper macro that prefixes every selected line with an apostrophe. All of these turn the selected lines green and make the compiler ignore them, so you can test the rest of a procedure without deleting anything.
Labels in the VBA editor can differ slightly between Office versions and languages, but the menus described here are the same across the desktop versions of Excel on Windows and Mac. The steps assume you are working in the Visual Basic Editor (press Alt+F11 on Windows, or Fn+Option+F11 on a Mac) and that the code is inside a standard module, a sheet module or ThisWorkbook. Nothing else is needed, and anything you comment can be uncommented later because the code itself is never removed.
What You'll Need
- Excel for Windows or Mac with macros enabled (a workbook saved as .xlsm, .xlsb or .xls if you want to keep the code).
- The Visual Basic Editor open, with the module containing the code visible.
- An understanding that commenting only stops the code running; it does not change what the macro does after you uncomment it again.
- An optional two-line helper macro if you want a permanent keyboard shortcut for the job (shown in step 7).
Step-by-Step
- Open the Visual Basic Editor and find the code. In Excel press Alt+F11 (Windows) or Fn+Option+F11 (Mac) to switch to the editor. In the Project Explorer on the left, double-click the module that holds your procedure, or use Ctrl+F / Cmd+F to search within the project. Make sure the code you want to disable is visible in the code window.
- Select the exact lines you want to disable. Click at the start of the first line, hold Shift, and click at the end of the last line. You can also click in the left margin and drag downward. Select whole statement lines only; do not include the line that continues a statement with an underscore (
_), or the code will break when you uncomment it later. If a statement wraps over several lines, select the whole wrapped statement. - Comment from the menu bar first. The quickest built-in route is the Edit toolbar: click View > Toolbars > Edit to show it, then click the Comment Block button (it looks like a small set of blue lines with a comment symbol). The selected lines are prefixed with an apostrophe and turn green. Click Uncomment Block on the same toolbar to reverse it.
- If the toolbar is missing, use the keyboard method. Type an apostrophe at the start of each line. Move to the start of the first selected line with Home, type
', then move down with the arrow keys and repeat. In the VBA editor you can select a block and use Ctrl+' on some layouts where the commenting tools are mapped, but the apostrophe method always works regardless of version or platform. - Know which character to use. An apostrophe (
') is the standard comment marker. A singleRemfollowed by a space also works, but it must be the first thing on the line and cannot follow code on the same line, so apostrophes are less error-prone when you are commenting a block that already contains mixed code. Inside a line of code after a statement, use an apostrophe and a space before your note, for exampleRange("A1").Value = 10 ' sets the value. - Check the colour change and run the rest. Commented lines appear in green by default. Press F5 or F8 to step through the procedure and confirm the remaining code runs as expected. If the commented block contained a declaration you still use elsewhere, Excel will raise a compile error at the first use, which tells you exactly which line has to be re-enabled.
- Build a reusable toggle macro (optional but fast). Add a new standard module, keep it in Personal.xlsb if you want it in every workbook, and add a short procedure that loops through the lines in the active selection and prefixes each one with an apostrophe, skipping lines that already start with one. Assign the macro to a keyboard shortcut through the macro options so one key press comments the selection and a second run uncomments it.
- Never use a block delimiter from another language. VBA does not support
/* */or triple-quoted blocks. If you paste C# or JavaScript code, convert it: comment each line with an apostrophe instead, or wrap the code in anIf False Then ... End Ifblock if you need to keep it inside a single procedure body. That construct is legal but can trip up GoTo labels and variable declarations inside it. - Save and test before closing. Press Ctrl+S. When you save as .xlsm, Excel keeps the commented code exactly as it is. Close and reopen the workbook, then run the main macro once to be sure the commented block is genuinely skipped and the rest still works.
Common Mistakes
- Commenting only the first line of an If block. You must comment out the opening
If ... Thenand the matchingEnd Iftogether, or the code will not compile. The same applies toFor ... Next,Do ... LoopandWith ... End With. - Leaving a dangling continuation. If a line ends with an underscore, the next line is part of the same statement. Commenting only one of the two lines produces a syntax error. Select both lines or neither.
- Using a smart apostrophe. If you type in Word or paste text from a web page, curly quotes can replace the straight apostrophe and VBA will not treat the line as a comment. Retype the character in the editor.
- Commenting declarations you still need. A commented
Dimline leaves variables undeclared, which only fails if your module usesOption Explicit. Either keep the declaration active or comment it and every use of the variable. - Assuming commented code cannot run. A line inside a string literal that happens to contain an apostrophe is still code. The apostrophe only comments when it appears outside quotation marks.
- Leaving temporary comments in production code. Commented blocks tend to pile up. Keep a version history in your source control or a dated copy of the workbook, and remove dead blocks once the change is confirmed.
Tips
- Use F8 to step line by line when testing; the highlighted line skips green lines, so you can see exactly what is being ignored.
- Disable the statements you want gone, but leave the commented block tidy: each line with its own apostrophe at column one is easiest to restore later.
- If you are testing an alternative version of a routine, comment the old block and write the new code below it with a dated note rather than editing the original.
- For quick debugging, comment out a whole procedure that calls another one, so you can run a single routine in isolation.
- The VBA editor remembers the last used toolbar and code window state per user, so once the Edit toolbar is visible it normally stays visible.
- Back up the workbook before large commented-out edits; VBA has no undo history that survives closing the editor.
Troubleshooting
The code still runs. Check that every line in the block begins with an apostrophe at column one. A line that begins with spaces followed by code is not commented. Select the block and look for green text; anything black is still live.
You get a compile error such as "Expected: expression". You have commented part of a multi-line statement or only one side of a paired block such as If/End If. Re-enable the matching lines or comment the entire structure.
The Comment Block button is greyed out. It only acts on a selection, so make sure text is selected in the code window first. If the Edit toolbar is not shown at all, use View > Toolbars and enable it, or fall back to typing apostrophes.
Everything is green and nothing runs. You probably commented the opening line of the procedure or a line the rest depends on. Place the cursor on the first executable line, press F8, and see where execution stops.
The toolbar is missing on a Mac. The Visual Basic Editor on macOS has its own menus, and the option may be under Edit rather than a toolbar. Selecting the lines and using the editor's comment command from the Edit menu works the same way.
The workbook will not keep the code. If the file is saved as .xlsx, macros and modules cannot be stored. Save as .xlsm before making substantial changes.
Frequently Asked Questions
Is there a keyboard shortcut for block comments in VBA?
There is no universal built-in shortcut that all versions share. The reliable method is the Edit toolbar's Comment Block button, an apostrophe typed at the start of each line, or a small helper macro assigned to a shortcut key through the Macro dialog.
Does commenting out code delete it?
No. The text stays in the module in full; it is simply ignored when the code compiles and runs. Remove the apostrophes, or click Uncomment Block, and the code is active again.
Can I comment out a whole subroutine at once?
Yes. Select every line from Sub to End Sub and comment them together. The procedure then disappears from the macro list because VBA cannot see it until it is uncommented.
What is the difference between an apostrophe and Rem?
Both create comments. The apostrophe is the standard and can follow code on the same line. Rem must start the line and needs a space after it, so it is less convenient for quickly disabling a block.
Why do commented lines turn green?
That is the editor's default syntax colouring for comments. If they stay black, the apostrophe was not recognised, usually because of a curly quote or a stray character before it.
Does commenting out code speed up a macro?
Only when the block contains full statements or loops that no longer execute. The commented text itself is simply skipped at compile time, so the gain comes from not running those instructions, not from the apostrophes.