Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

VBA Comment Block Shortcut: How to Comment Code Fast

· 6 min read · Home Automation

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

VBA has no built-in keyboard shortcut to comment or uncomment a whole block of code in one keystroke. The fastest official method is to use the Edit toolbar's Comment Block and Uncomment Block buttons, or to add those commands to the Quick Access Toolbar and trigger them with Alt+1 and Alt+2. If you want a true Ctrl-based shortcut, you need a tiny third-party add-in, because the VBA editor does not let you remap keys natively. This guide covers the toolbar method, the Quick Access Toolbar method, and the add-in route, plus what to do when the toolbar buttons are greyed out or missing.

What You'll Need

  • A Windows PC with Microsoft Excel, Word, Access, PowerPoint or another Office app that includes the Visual Basic Editor (VBE). The VBE is not available in Excel for Mac in the same form, so the toolbar steps below apply to Windows.
  • The Developer tab enabled, or any open VBA module so you can reach the editor. If the Developer tab is hidden, right-click the ribbon, choose Customize the Ribbon, and tick Developer in the right-hand list.
  • For the true keyboard shortcut route: a VBA add-in such as a free comment-block add-in, downloaded from a reputable source, saved as an .xlam or .xla file. Only use add-ins you trust, because VBA code runs with your permissions.

Step-by-Step

  1. Open the Visual Basic Editor. In Excel, press Alt+F11. In Word, press Alt+F11 as well. The editor opens in its own window.
  2. Make sure the Edit toolbar is visible. In the VBE menu, go to View > Toolbars and tick Edit. The Edit toolbar is a small floating or docked bar with cut, copy, paste and two comment icons.
  3. Select the lines you want to comment. Click at the start of the first line, then Shift+click at the end of the last line, or drag across them. You can select a whole procedure or just a few lines.
  4. Click the Comment Block button on the Edit toolbar. It looks like a page with a blue comment mark. Every selected line gets an apostrophe at the start, which turns it into a comment.
  5. To remove the comments, select the same lines and click the Uncomment Block button next to it. Each line loses one leading apostrophe.
  6. If you want a keyboard shortcut, add the commands to the Quick Access Toolbar. In the VBE, right-click the toolbar area and choose Customize, or use View > Toolbars > Customize. Switch to the Commands tab, select the Edit category, and find Comment Block and Uncomment Block.
  7. Drag Comment Block onto the Quick Access Toolbar, then drag Uncomment Block next to it. Close the Customize dialog.
  8. Press Alt+1 to run the first button on the Quick Access Toolbar and Alt+2 for the second. If you placed Comment Block first and Uncomment Block second, Alt+1 comments the selection and Alt+2 uncomments it. The numbering follows the order of the buttons, so if you add more buttons later, the numbers shift.
  9. For a genuine Ctrl shortcut, install a comment-block add-in. Save the add-in file to your Office add-in folder, then in Excel go to File > Options > Add-ins, choose Excel Add-ins in the Manage box, click Go, and browse to the file. Once loaded, the add-in supplies its own shortcut, usually Ctrl+Shift+C and Ctrl+Shift+U, but check the add-in's own documentation because the keys vary.
  10. Test on a small block first. Select three lines, press your chosen shortcut, and confirm the apostrophes appear. Then uncomment and confirm they disappear.

Menu labels and toolbar names can differ slightly between Office versions and between the 32-bit and 64-bit builds, so if a label is not exactly as written here, look for the closest match in the same menu.

Common Mistakes

  • Expecting Ctrl+/ or Ctrl+K to work. Those are shortcuts in other code editors, not in the VBA editor. The VBE has no default block-comment shortcut.
  • Clicking Comment Block with nothing selected. The button then does nothing, or comments only the current line depending on the version. Always select the full range first.
  • Using the toolbar buttons on lines that already contain an apostrophe. Commenting again adds a second apostrophe, and uncommenting removes only one, so the line stays commented. Remove extras manually.
  • Forgetting that Alt+1 and Alt+2 are positional. If you add a new button to the left of the comment buttons, the numbers change and your muscle memory breaks.
  • Installing an add-in from an untrusted site. VBA add-ins can read and change your files, so treat them like any other executable.

Tips

  • Keep the Edit toolbar docked at the top of the VBE so the comment buttons are always one click away.
  • Use the Quick Access Toolbar method if you want keyboard access without installing anything. It is the closest you get to a native shortcut.
  • Comment in blocks while debugging rather than deleting code. It is faster to uncomment than to retype.
  • Remember that VBA comments start with an apostrophe or with the Rem keyword. The toolbar uses apostrophes, which is the more common style.
  • If you share workbooks, note that add-ins are installed per user, not saved inside the workbook, so colleagues will not inherit your shortcut.

Troubleshooting

  • The Edit toolbar is missing. Go to View > Toolbars and tick Edit. If it still does not appear, the VBE window may be too narrow; widen it or undock the toolbar.
  • The Comment Block button is greyed out. Click inside the code window first. The buttons are disabled when the focus is on a project explorer or properties pane.
  • Alt+1 does nothing. Confirm the Quick Access Toolbar actually contains the command. Right-click the toolbar and choose Customize to check the list order.
  • The add-in shortcut does not fire. Check that the add-in is ticked in the Add-ins dialog and that another add-in has not claimed the same key combination. Disable other add-ins one at a time to isolate the conflict.
  • Comments appear on the wrong lines. Your selection probably started mid-line. Select from the very start of the first line to the end of the last.

Frequently Asked Questions

Is there a default keyboard shortcut to comment a block in VBA?

No. The VBA editor ships without one. You either click the toolbar buttons, map them to Alt+1 and Alt+2 via the Quick Access Toolbar, or install an add-in that provides Ctrl-based keys.

Can I use Ctrl+/ like in other editors?

Not natively. The VBE does not allow key remapping, so Ctrl+/ only works if an add-in assigns it.

Does this work on Mac?

The VBE on Mac is more limited and the toolbar layout differs. The Quick Access Toolbar route described here is for Windows. On Mac, use the comment buttons if your version shows them, or add apostrophes manually.

Will commenting a block change how the code runs?

Yes, commented lines are ignored by VBA. That is the point, but it also means a commented-out line cannot cause an error or run a command.

Can I comment multiple procedures at once?

Yes, as long as they are contiguous and selected in the same code window. The toolbar applies to the whole selection.

Why does uncommenting leave an apostrophe behind?

Because the line had two apostrophes. Uncomment Block removes one per click, so click it again or delete the extra apostrophe manually.