Shortcut Key to Comment/Uncomment Blocks of Code in VBA

You don't need 3rd-party tools to add keyboard shortcuts for commenting and uncommenting code blocks in VBA with this trick.

Shortcut Key to Comment/Uncomment Blocks of Code in VBA

One thing that is conspicuously missing from the VBA IDE is a keyboard shortcut to comment and uncomment blocks of code.

The VBIDE does provide commands for commenting and uncommenting blocks of code, though.  And by adding those commands to the menu bar, we can assign "accelerator" keys to make it easy to use the keyboard to trigger these two commands.  "Accelerator" keys are the underlined letters in menu commands that you can use to massively boost your productivity for frequently used commands.

Follow the steps below to assign the following shortcut keys to comment and uncomment blocks of VBA code:

  • [Alt] + [C]: comment code block
  • [Alt] + [U]: uncomment code block

Step-by-Step: Adding Comment/Uncomment Block Shortcut Keys

  1. Enter Customize mode: View > Toolbars > Customize... ([Alt] + [V], [T], [C])
  2. Go to the "Commands" tab
  3. Go to the "Edit" category
  4. Scroll to "Comment Block" command in Commands list
  5. Click and drag "Comment Block" to the end of the toolbar
  6. Click and drag "Uncomment Block" to the end of the toolbar
  7. Right-click the "Comment Block" icon and set the name to "&Comment Block"
  8. Check the box next to "Image and Text"
  9. Repeat steps for "Uncomment Block" and set name to "&Uncomment Block"

You can now comment blocks of code with [Alt] + [C] and uncomment blocks with [Alt] + [U].

Screenshots

All original code samples by Mike Wolfe are licensed under CC BY 4.0