How to comment and uncomment blocks of code in the Office VBA Editor

VbaComments

Vba Problem Overview


In the VBA editor of Office (ALT + F11), how do you comment or uncomment a block of code?

Vba Solutions


Solution 1 - Vba

In the VBA editor, go to View, Toolbars, Customise... or right click on the tool bar and select Customise...

Under the Commands tab, select the Edit menu on the left.

Then approximately two thirds of the way down there's two icons, Comment Block and Uncomment Block.

Drag and drop these onto your toolbar and then you have easy access to highlight a block of code, and comment it out and uncomment with the click of a button!


See GauravSingh's answer if you want to assign keyboard shortcuts.

Solution 2 - Vba

  1. Right-click on the toolbar and select Customize...
  2. Select the Commands tab.
  3. Under Categories click on Edit, then select Comment Block in the Commands listbox.
  4. Drag the Comment Block entry onto the Menu Bar (yep! the menu bar)
    Note: You should now see a new icon on the menu bar.
  5. Make sure that the new icon is highlighted (it will have a black square around it) then
    click Modify Selection button on the Customize dialog box.
  6. An interesting menu will popup.
    Under name, add an ampersand (&) to the beginning of the entry.
    So now instead of "Comment Block" it should read &Comment Block.
    Press Enter to save the change.
  7. Click on Modify Selection again and select Image and Text.
  8. Dismiss the Customize dialog box.
  9. Highlight any block of code and press Alt-C. Voila.
  10. Do the same thing for the Uncomment Block or
    any other commands that you find yourself using often.

Solution 3 - Vba

There is a built-in Edit toolbar in the VBA editor that has the Comment Block and Uncomment Block buttons by default, and other useful tools.

If you right-click any toolbar or menu (or go to the View menu > Toolbars), you will see a list of available toolbars (above the "Customize..." option). The Standard toolbar is selected by default. Select the Edit toolbar and the new toolbar will appear, with the Comment Block buttons in the middle.

enter image description here

*This is a simpler option to the ones mentioned.

Solution 4 - Vba

Have you checked MZTools?? It does a lot of cool stuff...

If I'm not wrong, one of the functionalities it offers is to set your own shortcuts.

Solution 5 - Vba

Or just click View, ToolBars, Edit. Then you can select a block of code and then click the Comment or Uncomment toolbar button to do everything in one click.

As an aside, you can Tab/Shift+Tab a block of selected text also. When I was a noobie, I didn't know that for a long time and would do them one line at a time.

Good Luck!

Solution 6 - Vba

After adding the icon to the toolbar and when modifying the selected icon, the ampersand in the name input is specifying that the next character is the character used along with Alt for the shortcut. Since you must select a display option from the Modify Selection drop down menu that includes displaying the text, you could also write &C in the name field and get the same result as &Comment Block (without the lengthy text).

Solution 7 - Vba

An easy way to add buttons to Comment or Un-Comment a code block is:

  • Go to View-Toolbars-Customise
  • Select the Command tab
  • Select the Edit Category on the left
  • Drag the “Comment Block” and “Uncomment Block” icons onto your toolbar.

Solution 8 - Vba

Steps to comment / uncommented

Press alt + f11/ Developer tab visual basic editor view tab - toolbar - edit - comments.

Solution 9 - Vba

With MZ-Tools installed, I comment/uncomment blocks in VBE by using the keyboard shortcut
Ctrl+Alt+C (MZ-Tools default)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRemarkLimaView Question on Stackoverflow
Solution 1 - VbaRemarkLimaView Answer on Stackoverflow
Solution 2 - VbaGauravSinghView Answer on Stackoverflow
Solution 3 - VbadePatinkinView Answer on Stackoverflow
Solution 4 - VbaTiago CardosoView Answer on Stackoverflow
Solution 5 - Vbaj2associatesView Answer on Stackoverflow
Solution 6 - VbaSasha DorvalView Answer on Stackoverflow
Solution 7 - VbaAaron LelevierView Answer on Stackoverflow
Solution 8 - VbaRohan BellimalView Answer on Stackoverflow
Solution 9 - Vba6diegodiego9View Answer on Stackoverflow