How to Comment a Block of Code in VBA (With Example)


You can use a single quotation ( ) in VBA to comment out a single line of code.

However, there is no built-in option in VBA to comment out a block of code.

Fortunately, there is a neat way to create a shortcut key that you can use to comment out a block of code and the following step-by-step example shows how.

Step 1: Customize the Toolbar

Within the VB Editor, right click anywhere on the toolbar and then click Customize:

Step 2: Add Comment Block to Toolbar

Click the Commands tab, then under Categories click Edit, then under Commands click Comment Block.

Then drag the Comment Block option onto the Toolbar. An icon should now appear for the comment block on the toolbar:

Step 3: Create Comment Block Shortcut Key

Next, click the Comment block icon on the toolbar (a black box will appear around the icon).

In the Customize panel, click the Modify Selection dropdown, then in the Name field type an ampersand ( & ) symbol in front of Comment Block and press Enter:

Then click the Modify Selection dropdown again and click Image and Text from the options:

VBA comment block shortcut keys

Then click Close on the Customize window.

You will now be able to highlight a block of code and press Alt + C to comment out the entire block.

Step 4: Use the Comment Block Shortcut Key

Suppose we would like to comment out the entire block of code in the following sub procedure:

Once we’ve highlighted this block of code, we can simply press Alt + C and the entire block will be commented out:

VBA comment block

Note that you can also create a shortcut key to uncomment a block of code by repeating the same steps above for the Uncomment Block option.

Once you’ve done so, you will then be able to highlight a block of code and press Alt + U to uncomment out the entire block.

Additional Resources

The following tutorials explain how to perform other common tasks in VBA:

How to Create Folders Using VBA
How to Delete Folders Using VBA
How to Delete Files Using VBA
How to Check if File Exists Using VBA

3 Replies to “How to Comment a Block of Code in VBA (With Example)”

  1. Hi thanks for this. Wondering if you could give a quick explain how this results in mapping Alt+C and Alt+U as expected? It works for me but I’m scratching my head as to why. Much appreciated. Cheers.

    1. Sure thing! In VBA (Visual Basic for Applications), there isn’t a built-in shortcut to comment or uncomment blocks of code directly like in some other IDEs. However, we can achieve this by creating custom macros that are then assigned to specific keyboard shortcuts (e.g., Alt+C for commenting and Alt+U for uncommenting).

      Here’s a quick breakdown of why Alt+C and Alt+U work as expected:

      1. **Macros to Comment/Uncomment Code:**
      You likely have set up two macros: one for commenting code and another for uncommenting it. These macros usually loop through the selected lines of code and add or remove the comment character (`’`) at the start of each line.

      Here’s a simplified version of what those macros might look like:

      – **Comment Macro** (assigned to Alt+C):
      “`vba
      Sub CommentCode()
      Dim Line As Variant
      For Each Line In Selection.Lines
      Line.Text = “‘” & Line.Text
      Next Line
      End Sub
      “`

      – **Uncomment Macro** (assigned to Alt+U):
      “`vba
      Sub UncommentCode()
      Dim Line As Variant
      For Each Line In Selection.Lines
      If Left(Line.Text, 1) = “‘” Then
      Line.Text = Mid(Line.Text, 2)
      End If
      Next Line
      End Sub
      “`

      2. **Assigning Keyboard Shortcuts:**
      In the VBA editor, you can assign these macros to specific keyboard shortcuts through the “Customize” menu (accessible by right-clicking the toolbar and choosing “Customize”). When assigning shortcuts, you can map Alt+C to the `CommentCode` macro and Alt+U to the `UncommentCode` macro.

      3. **Why Alt+C and Alt+U Work:**
      The reason Alt+C and Alt+U trigger the expected behavior is because they have been explicitly mapped to those specific macros. Whenever you press Alt+C, the VBA editor recognizes the shortcut and runs the `CommentCode` macro, adding comment characters to the start of each selected line. Similarly, pressing Alt+U runs the `UncommentCode` macro, removing the comment characters.

      4. **Shortcut Behavior:**
      The VBA editor allows you to map macros to keyboard shortcuts, enabling you to automate repetitive tasks (like commenting/uncommenting code blocks) with a simple key press. This feature doesn’t require the shortcuts to be predefined in VBA itself; it’s more about setting up the macro-to-shortcut mapping correctly.

      So, the key points are:
      – You have macros for commenting and uncommenting code.
      – These macros are mapped to Alt+C and Alt+U.
      – Pressing Alt+C or Alt+U triggers the macros, which modify the selected code lines as intended.

      If you need any more details or want to dive deeper into how to set up these macros and assign shortcuts, feel free to ask! Cheers! 🍻

Leave a Reply

Your email address will not be published. Required fields are marked *