Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

comments

vba

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

like image 375
RemarkLima Avatar asked Oct 17 '12 11:10

RemarkLima


People also ask

How do you comment out multiple lines in VBA?

All you have to do is to select all lines of code you want to comment or uncomment. To make this option visible go to View – Toolbars – and tick the Edit option. A new set of options will appear in the toolbar. Among them you will find the Comment/Uncomment buttons, as you can see in the image.

How do you comment or uncomment a whole block of code at once?

Comment Code Block Ctrl+K+C/Ctrl+K+U If you select a block of code and use the key sequence Ctrl+K+C, you'll comment out the section of code. Ctrl+K+U will uncomment the code.


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

like image 191
RemarkLima Avatar answered Oct 29 '22 05:10

RemarkLima


  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.
like image 33
GauravSingh Avatar answered Oct 29 '22 05:10

GauravSingh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!