334

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

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
RemarkLima
  • 11,639
  • 7
  • 37
  • 56
  • 4
    @AlistairWeir the alternative to the `'` (*single quote*) is to use a keyword `Rem`. –  Aug 20 '13 at 08:55

8 Answers8

546

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.

Community
  • 1
  • 1
RemarkLima
  • 11,639
  • 7
  • 37
  • 56
  • 15
    That'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
  • 3
    Same 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
  • 3
    Another 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
  • 5
    Olle: 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
  • GauravSingh takes it further in the answer below. Nice! – Pecos Bill Jul 08 '15 at 17:20
  • What about normal keyboard shortcut? – Do-do-new Jun 28 '18 at 16:09
  • 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
183
  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.
GauravSingh
  • 1,955
  • 1
  • 12
  • 11
  • 13
    Though 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
    @mrquad - See step 10 so Alt-U means Uncomment Block – Martin F Mar 30 '15 at 19:41
  • 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
  • 3
    In 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
  • I shortened the name to "&Cmnt" @Air – Forester Nov 25 '16 at 01:18
  • 9
    One of the most mindblowing answer I have seen on VBE. +1 – kolcinx Jul 13 '17 at 19:37
  • 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 keyboard shortcut for uncommenting is Alt+U – Donald Li Mar 15 '21 at 16:09
  • 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
  • @lun, did you find out step 5 yet? – Timo May 08 '21 at 18:01
  • is the menubar the toolbar used in the accepted answer? – Timo Sep 14 '21 at 15:12
  • 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
47

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.

dePatinkin
  • 2,239
  • 1
  • 16
  • 15
20

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.

Tiago Cardoso
  • 2,057
  • 1
  • 14
  • 33
4

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!

Marcus Mangelsdorf
  • 2,852
  • 1
  • 30
  • 40
j2associates
  • 1,115
  • 10
  • 19
3

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).

Sasha Dorval
  • 45
  • 1
  • 8
1

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.
Aaron Lelevier
  • 19,850
  • 11
  • 76
  • 111
1

Steps to comment / uncommented

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