Many languages, such as T-SQL, have a set of characters for single-line comments (e.g.,
--) and a different set of characters for multi-line/block comments (e.g.,
-- This is a single line comment in T-SQL /* This is a block comment in T-SQL */
VBA supports single-line comments only:
'This is a single-line comment in VBA
"Block comments" in VBA are nothing more than a series of single-line comments:
' This is a ' block ' comment ' in VBA
Adding and Removing Multiple Single-Line Comments
Built-in Block Comment Support (no shortcut key)
The VBA editor has block comment/block uncomment buttons, but they are hidden by default. They appear on the Edit toolbar. To show this toolbar, go to View > Toolbars > Edit:
Unfortunately, there is no shortcut key assigned to these buttons. If you want to assign a shortcut key to them (and you will), you'll either need a third-party tool or a
kludgy hack clever workaround.
Shortcut Key Support via MZ-Tools
The VBA editor is ancient, so there are not even any built-in shortcut keys for block commenting chunks of code. Many developers (myself included) use the block comment feature built into the VBA utility, MZ-Tools.
Shortcut Key Workaround without 3rd Party Tools
Another alternative is to create a custom shortcut key to add block comments to your VBA code as I described in an earlier article:
Quick and Dirty Alternative: Conditional Compile Statements
It's an admittedly ugly hack, but you can get T-SQL-style multi-line comment blocks by (ab)using conditional compilation:
#If False Then This is a block comment in VBA #End If
Conditional compile statements are evaluated before the code is (pseudo-)compiled. They are most useful for allowing your code to support multiple compile environments, such as 32-bit vs. 64-bit or VBA6 vs. VBA7.
How it Works
Before the compiler evaluates your code, it checks any conditional compile statements (i.e., those that start with the
If the condition evaluates to true, then the code within the conditional compile block is "compiled." If the condition evaluates to false, then the compiler completely ignores the code within the block.
By hard-coding the value
False in the above conditional compile statement, we guarantee that the compiler will ignore everything that follows up to the corresponding
#End If statement.
Why You Shouldn't Use It
I call this a "quick and dirty" alternative for a reason.
It Hurts Readability
You get no code coloring. In fact, you get no visual indication of any kind that the code you are looking at isn't being compiled.
Which means that other developers won't be expecting it. If they do manage to figure out what's going on, they will wonder if even you knew what you were doing.
There are Better Alternatives
Like the two I listed above:
Why Would You Use It?
You're Away From Your Custom Dev Environment
Troubleshooting some VBA code on an end-user's computer or helping a colleague with a programming question? This is a no-setup-required solution for those times when you don't have access to your custom shortcut key or a copy of MZ-Tools.
You Want to Quickly Comment Out LARGE Blocks of Code
The block-comment tools can be a bit of an adventure when it comes to commenting–and especially uncommenting–large blocks of code. If you're not careful, it can be easy to comment or uncomment more code than you meant to. The conditional compilation approach neatly avoids those issues.
UPDATE [2022-06-26]: Added section on the built-in Edit toolbar available via View > Toolbars > Edit. (h/t Neil Sargent in the comments below, and several other readers who emailed me with similar information, including Klaus Oberdalhoff)