In the VBA editor of Office (ALT + F11), how do you comment or uncomment a block of code?
-
4@AlistairWeir the alternative to the `'` (*single quote*) is to use a keyword `Rem`. – Aug 20 '13 at 08:55
8 Answers
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.

- 1
- 1

- 11,639
- 7
- 37
- 56
-
15That's handy - I can't believe I'd never found it before. A shortcut key would be really handy but unfortunately that doesn't seem to exist. – Jamie Bull Oct 17 '12 at 12:26
-
3Same here. Hence thinking it's worthy of being posted! All these years of using the [poor] VBA editor, and something that is basically essential for decent testing, and it was there all along :) – RemarkLima Oct 17 '12 at 12:40
-
3Another button that belongs next to them (even though it's a little easier to find) is the Compile VBAProject button from the Debug menu. It's the three commands I use the most! – Olle Sjögren Oct 17 '12 at 16:07
-
5Olle: Right-click the menu bar, choose Customize (or choose View | Toolbars | Customize). Click the Commands tab. Drag any command you want to any toolbar you'd like to have it on. Bless whoever at MS didn't care enough about VBA to "improve" the UI the way they did the main apps. – Steve Rindsberg Oct 18 '12 at 15:14
-
Or just right click on the toolbar and turn on the Edit toolbar - the Comment and Uncomment buttons are included there. – Brian Burns Apr 13 '15 at 18:05
-
-
-
1@Empus just have a look at the other answer, https://stackoverflow.com/a/23954017/1281576 which shows how to create a Keyboard Short (not sure what you by "normal"?) – RemarkLima Jun 28 '18 at 16:12
-
@RemarkLima Thanks, haven't notice it before! Saying normal I implied that all editors which I worked with (from Emacs to VS Code) have a shortcut for it... Never had to work with VBA in Excel before, so... – Do-do-new Jun 28 '18 at 17:43
- 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.

- 458
- 10
- 27

- 1,955
- 1
- 12
- 11
-
13Though this answers is same as correct answer, he has shown a trick to add shortcut. +1 for that. – Barshan Das Oct 14 '14 at 06:15
-
8
-
Could I somehow change to combination? I'd like to use "ctrl+(k,c)" and "ctrl+(k,u)" like in SQL Server Management Studio. – jumxozizi May 31 '16 at 10:00
-
3In Office 2010, this also works if you choose "Text Only (Always)" in Step 7, rather than "Image and Text." It's obnoxious that it doesn't work if you choose any other display option, but at least on my screen I prefer to save a little space with "Text Only." – Air Jun 06 '16 at 18:30
-
This also works if you want to use a non-letter symbol like `=`. Just prefix the name with `&=`. – Concerned_Citizen Oct 17 '16 at 00:53
-
-
9
-
I didn't know this! Absolutely amazing! Now I can Alt+C to comment and Alt+U to uncomment, and more possibilities! – Nicholas Humphrey May 23 '18 at 17:45
-
This answer is a testament to how convoluted UI designed back in the day. And it's still here, 2019. – Tam Le Aug 07 '19 at 09:45
-
Now, how do I remove this from menu bar. I dragged the same icon 2 times. Answer: While the 'customize' window is open, drag out the button/icon from its bar. – YetAnotherBot Aug 13 '19 at 06:44
-
I like this answer more than the accepted one, since it explains how to get a shortcut. – 3UqU57GnaX May 22 '20 at 09:16
-
I dont understand step 5 - make sure the icon is highlighted. Highlighted where? Could you show me a screenshot? @GauravSingh – Lun Oct 04 '20 at 05:30
-
1
-
The `uncomment block` does not work because `alt+u` refers to a menu entry, in german the menu `ausführen` whjch uses `u`. What can I do? – Timo May 08 '21 at 17:50
-
In `office 365proplus` the way to make shortcut is slightly different, you need the left button and not the right one. [Here](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/customize-dialog-box), the right one is used. – Timo May 08 '21 at 17:53
-
-
-
1@Timo The "U" is bound to "A_u_sführen" in German. But you can remove the selection the same way you added it to "Uncomment". That means: Rightclick the menu point you want to change (here "Ausführen"), select "Customize..." ("Anpassen...") and then change "A&usführen" to "Ausführen". Then ALT + U will work the way you want it to. – TimFinnegan Dec 22 '22 at 20:06
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.

- 2,239
- 1
- 16
- 15
-
Sorry, somehow I missed your answer when I posted the same thing below. – j2associates Jun 28 '15 at 00:54
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.

- 2,057
- 1
- 14
- 33
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!

- 2,852
- 1
- 30
- 40

- 1,115
- 10
- 19
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).

- 45
- 1
- 8
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.

- 19,850
- 11
- 76
- 111
Steps to comment / uncommented
Press alt + f11/ Developer tab visual basic editor view tab - toolbar - edit - comments.

- 11
- 1