ChatGPT解决这个技术问题 Extra ChatGPT

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?

@AlistairWeir the alternative to the ' (single quote) is to use a keyword Rem.

C
Community

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.


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.
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 :)
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: 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.
@Empus just have a look at the other answer, stackoverflow.com/a/23954017/1281576 which shows how to create a Keyboard Short (not sure what you by "normal"?)
I
I say Reinstate Monica

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.


Though this answers is same as correct answer, he has shown a trick to add shortcut. +1 for that.
@mrquad - See step 10 so Alt-U means Uncomment Block
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."
One of the most mindblowing answer I have seen on VBE. +1
The keyboard shortcut for uncommenting is Alt+U
d
dePatinkin

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.

https://i.stack.imgur.com/aJeN2.jpg

*This is a simpler option to the ones mentioned.


Sorry, somehow I missed your answer when I posted the same thing below.
T
Tiago Cardoso

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.


Yeap, it's lets you add keyboard shortcuts
M
Marcus Mangelsdorf

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!


S
Sasha Dorval

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


A
Aaron Lelevier

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.


This is the same as the correct answer, and more than a year later.
R
Rohan Bellimal

Steps to comment / uncommented

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


6
6diegodiego9

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