Excel VBA has no special multi-line block-comment syntax such as /* … */. To comment out several lines, select them in the Visual Basic Editor and use Edit > Comment Block, or put an apostrophe (‘) at the start of each line; use Edit > Uncomment Block to restore selected lines.
What a block comment means in VBA
A block comment is a group of lines treated as notes rather than executable code. In VBA, the apostrophe marks the rest of its line as a comment. Because that marker works line by line, a block comment is simply a run of lines that each begin with an apostrophe. For example, three commented lines are three separate comments, not one comment spanning a range.
This distinction matters when moving examples from other programming languages. VBA does not recognize C-style /* and */ markers or JavaScript-style // comments. Those characters do not turn a whole region into a comment; depending on where they appear, they can instead produce a compile error. The VBA editor’s Comment Block command is a convenience that inserts apostrophes for you. It does not create a different kind of comment in the language.
Comments are ignored when VBA runs, so they do not change the workbook’s values or control flow. They are useful for documentation, temporarily disabling a small section while troubleshooting, or keeping an alternative statement nearby. They are not a substitute for removing obsolete code permanently when that code is no longer needed.
Use Comment Block in the Visual Basic Editor
In Excel for Microsoft 365 on Windows, open the VBA editor with Alt+F11, then open the module containing the code. If the Edit toolbar is not visible, choose View > Toolbars > Edit in the Visual Basic Editor. Select the lines you want to comment and click Comment Block. To reverse the operation, select the commented lines and click Uncomment Block.
- Open the relevant workbook and the Visual Basic Editor. On Windows, press Alt+F11. On a Mac, Option+F11 may open the editor; on some keyboards you may also need to hold Fn.
- In the Project Explorer, open the module, worksheet, or workbook code window that contains the lines. If the Project Explorer is hidden, use View > Project Explorer in the editor.
- Drag across the lines in the code pane. Make sure the selection includes every line you intend to disable, including any continuation lines that belong to the same VBA statement.
- Choose Comment Block on the Edit toolbar. Review the inserted apostrophes and compile or run the procedure only when the surrounding code is in a valid state.
The Comment Block and Uncomment Block buttons are commands, not a single automatic toggle: choose the one that matches the current state of the selected lines. If you cannot find the buttons, check that the Edit toolbar is shown. The Visual Basic Editor’s menus and toolbar appearance can vary slightly between Office installations, but the commands belong to the editor, not to Excel’s worksheet ribbon.
Comment lines manually with apostrophes
For a short region, typing an apostrophe at the beginning of each line is often the quickest and clearest method. In a VBA code pane, the apostrophe usually appears in green because the editor formats comments differently from code. A comment can also follow a statement on the same line, but putting comments on their own lines makes a disabled block easier to see and restore.
Sub ShowCommentExample()
Dim message As String
message = "The active line still runs."
' message = "This line is temporarily disabled."
' MsgBox message
End Sub
In this example, the first assignment runs, while the two apostrophe-prefixed lines are ignored. The procedure still has a declared variable, and the commented-out statements remain visible for reference. If you uncomment the second assignment without also commenting out the first, the later assignment replaces the earlier value; that may be intentional, but it is worth checking the logic before restoring a block.
VBA also supports the keyword Rem as a comment marker. For example, a line can begin with Rem followed by a space and the comment text. The apostrophe is more common in editor code and works conveniently for inline comments, so it is generally the simpler marker to use when commenting a selected block. Do not mix markers unnecessarily; consistent apostrophe comments are easier to scan and remove with the editor command.
What the editor commands change
Comment Block adds an apostrophe to each selected line, including lines that may already contain code or be blank. Uncomment Block removes a leading comment marker from selected lines when it can identify one. It is not a general-purpose cleanup command: if the selected lines contain comments that were already present before you used Comment Block, removing a marker can change the original text. Keep the selection limited to the block you just commented, and inspect the result before running the procedure.
Suppose a procedure contains a three-line statement, each line selected as part of a larger block. The editor prefixes each selected line, turning the statement into comments line by line. Selecting only part of a logical statement can leave the remaining code incomplete and cause a compile error. When in doubt, select the entire statement or the entire group of statements, not just the line that appears to contain the main action.
Blank lines and indentation are mostly a readability issue, but selection boundaries affect what gets changed. A reliable habit is to include only the intended code lines, apply the command, then look at the first and last lines of the selection. For especially important code, copy the original lines elsewhere in the module or save a separate workbook copy before editing.
Commenting out code while troubleshooting
Commenting out a section can help isolate a problem: temporarily disable a suspected statement, run the procedure, and see whether the behavior changes. Change one small section at a time. If you comment out a large procedure all at once, you may remove both the cause of the problem and code the rest of the routine needs, making the result harder to interpret.
For example, if an error seems to occur during a sequence of worksheet updates, temporarily comment out one update at a time rather than disabling the whole routine. Record what you changed, test the relevant case, and restore the code promptly when the test is complete. A comment such as “Temporarily disabled while checking the import step” can help explain why a line is inactive, but remove that note when you restore the working code.
Remember that a commented line does not execute, so any variables, object assignments, or setup steps on that line are absent too. Disabling one line can therefore cause a later line to fail because it expects a value or object to have been set. A new error after commenting out code does not necessarily mean the edit itself is malformed; it may reveal a dependency between statements.
VBA comments are not a safety switch
Apostrophe comments affect how VBA reads source code; they do not provide a security boundary or a way to disable all macros in a workbook. They also do not change the workbook’s macro security settings. If the goal is to prevent macros from running, use Excel’s security controls and handle the workbook as untrusted where appropriate, rather than relying on comments in one module.
Likewise, comments are not a dependable way to hide sensitive information. Anyone with access to the VBA project may be able to read comments and code. Do not leave passwords, access tokens, or private data in comments. If a value must be protected, use an appropriate secure system rather than storing it in workbook source code.
Before running a macro that changes workbook data, save a copy of the workbook. Excel macros cannot be undone with Ctrl+Z, even if the macro’s visible action seems small. That warning applies when testing code after a comment edit: commenting is reversible in the editor, but the effects of running the restored macro may not be.
Common mistakes and how to fix them
- Typing /* and */ around VBA. VBA does not use those as block-comment delimiters. Prefix each line with an apostrophe or use Comment Block.
- Uncommenting too much. Select only the lines that were commented as a block. Then inspect the result, because some lines may have had apostrophe comments before the block command was used.
- Leaving part of a statement active. If a statement spans several lines, comment out all of its lines. Otherwise the remaining fragment may not compile.
- Forgetting a dependent setup line. A later statement may depend on a variable or object that the disabled code created. Check the surrounding procedure when testing.
- Expecting a worksheet shortcut. Comment Block is a Visual Basic Editor command. Excel’s normal worksheet shortcuts do not apply in the same way, and the editor does not provide a universal default keyboard shortcut for this command.
- Trying to run VBA in Excel for the web. Excel for the web can open a macro-enabled workbook, but it cannot create, edit, or run VBA macros. Use desktop Excel to work with the VBA editor.
Alternatives to commenting out a block
For a one-time test, a breakpoint or stepping through the code may be more informative than disabling statements. In the Visual Basic Editor, click beside a code line to set a breakpoint, then run the procedure and inspect where execution pauses. This preserves the code and lets you examine variable values and the sequence of execution. It is a debugging tool, not a replacement for a comment when you specifically need code not to run.
For a lasting change, delete genuinely obsolete code after confirming it is no longer needed, and use version history or a saved copy if you need a recovery path. Keeping long-term unused code commented out can make modules confusing: readers may not know whether it is a deliberate fallback, unfinished work, or forgotten debris. A short explanation and a clear cleanup decision are more useful than an indefinitely growing block of disabled statements.
Conditional compilation is another VBA feature, but it is not usually the best substitute for an ordinary block comment. Directives such as #If are intended to include or exclude code based on compilation conditions, such as a platform or declared conditional constant. They require matching directives and can make code harder to follow. For routine temporary troubleshooting, the apostrophe method is simpler.
Frequently Asked Questions
Does VBA have a block-comment shortcut?
VBA has Comment Block and Uncomment Block commands in the Visual Basic Editor, but there is no universal default keyboard shortcut for them. Show the Edit toolbar, select the lines, and click the relevant command.
How do I comment multiple lines in Excel VBA?
Select the lines in the VBA editor and choose Comment Block, or type an apostrophe at the start of every line. To restore a block created that way, select those lines and choose Uncomment Block.
Can I use /* … */ in VBA?
No. That syntax is not VBA’s multi-line comment format. Use apostrophes on each line, or use the editor’s Comment Block command to insert them automatically.
Can I comment out only part of a VBA line?
Yes. An apostrophe marks the rest of that line as a comment, so it can follow a statement. For temporarily disabling whole statements, separate comment lines are usually clearer and easier to restore.
Can Excel for the web edit or run VBA?
No. Excel for the web does not provide the VBA editor or run VBA macros. Open the workbook in desktop Excel for Windows or Mac to edit and run its VBA code.
Related
- Excel Accounting vs Currency: Key Differences Explained
- Excel Data Model Won’t Open? Causes and Fixes
- Excel Data Model vs Power Query: How They Work Together
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Input Box in Excel VBA: Use InputBox and Get DataIn Excel VBA, use InputBox to ask for a text value, or Application.InputBox when you want to restrict…
- Excel Macro Not Working? Find and Fix Common CausesIf an Excel macro is not working, first confirm that the workbook is saved in a macro-enabled format,…
- Excel Macro Commands List: Common VBA Actions ExplainedAn Excel macro command is a Visual Basic for Applications (VBA) instruction that tells Excel what to do,…
- VBA Array Size: Practical Limits, Bounds, and Memory UseThere is no single fixed maximum size for a VBA array: its practical limit depends on available memory,…
- VBA Range Column: How to Reference ColumnsIn VBA, reference a whole worksheet column with Columns("C") or Range("C:C"); to get the column containing a particular…
- Excel VBA Message Box: How to Use MsgBox in MacrosIn Excel VBA, use the MsgBox function to show a message, optionally display buttons or an icon, and…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.