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

在Office的VBA编辑器(ALT + F11)中,您如何评论或取消注释一段代码?


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.


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

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

    链接地址: http://www.djcxy.com/p/20588.html

    上一篇: 如何在Ruby中生成一个随机字符串

    下一篇: 如何评论和取消注释Office VBA编辑器中的代码块