How to comment and uncomment blocks of code in the Office VBA Editor
In the VBA editor of Office (ALT + F11), how do you comment or uncomment a block of code?
Solution 1:
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:
- Right-click on the toolbar and select Customize...
- Select the Commands tab.
- Under Categories click on Edit, then select Comment Block in the Commands listbox.
- 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. - 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. - 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. - Click on Modify Selection again and select Image and Text.
- Dismiss the Customize dialog box.
- Highlight any block of code and press Alt-C. Voila.
- Do the same thing for the Uncomment Block or
any other commands that you find yourself using often.
Solution 3:
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.
*This is a simpler option to the ones mentioned.