如何在 Office VBA 编辑器中注释和取消注释代码块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12933279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to comment and uncomment blocks of code in the Office VBA Editor
提问by RemarkLima
In the VBA editor of Office (ALT+ F11), how do you comment or uncomment a block of code?
在Office(ALT+ F11)的VBA编辑器中,如何注释或取消注释一段代码?
回答by RemarkLima
In the VBA editor, go to View
, Toolbars
, Customise...
or right click on the tool bar and select Customise...
在VBA编辑器,转到View
,Toolbars
,Customise...
或在工具栏上点击右键并选择Customise...
Under the Commands
tab, select the Edit
menu on the left.
在Commands
选项卡下,选择Edit
左侧的菜单。
Then approximately two thirds of the way down there's two icons, Comment Block
and Uncomment Block
.
然后大约三分之二的位置有两个图标,Comment Block
和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 answerif you want to assign keyboard shortcuts.
如果要分配键盘快捷键,请参阅GauravSingh 的回答。
回答by GauravSingh
- Right-click on the toolbar and select Customize...
- Select the Commandstab.
- Under Categoriesclick on Edit, then select Comment Blockin the Commandslistbox.
- Drag the Comment Blockentry 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 Selectionbutton on the Customizedialog 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 Enterto save the change. - Click on Modify Selectionagain and select Image and Text.
- Dismiss the Customizedialog 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.
- 右键单击工具栏并选择自定义...
- 选择命令选项卡。
- 在类别下单击编辑,然后在命令列表框中选择注释块。
- 将Comment Block条目拖到菜单栏上(是的!菜单栏)
注意:您现在应该在菜单栏上看到一个新图标。 - 确保新图标突出显示(它周围会有一个黑色方块),然后
单击“自定义”对话框上的“修改选择”按钮。 - 会弹出一个有趣的菜单。
在名称下,&
在条目的开头添加一个和号 ( )。
因此,现在应该阅读&Comment Block而不是“Comment Block” 。
按Enter保存更改。 - 再次单击修改选择并选择图像和文本。
- 关闭自定义对话框。
- 突出显示任何代码块并按Alt- C。瞧。
- 对 Uncomment Block 或
您发现自己经常使用的任何其他命令执行相同的操作。
回答by dePatinkin
There is a built-in Edittoolbar in the VBA editor that has the Comment Blockand Uncomment Blockbuttons by default, and other useful tools.
VBA 编辑器中有一个内置的“编辑”工具栏,默认情况下具有“注释块”和“取消注释块”按钮,以及其他有用的工具。
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 Edittoolbar and the new toolbar will appear, with the Comment Block buttons in the middle.
如果右键单击任何工具栏或菜单(或转到“视图”菜单 >“工具栏”),您将看到可用工具栏的列表(在“自定义...”选项上方)。默认情况下选择标准工具栏。选择编辑工具栏,新工具栏将出现,中间有注释块按钮。
*This is a simpler option to the ones mentioned.
*与上述选项相比,这是一个更简单的选项。
回答by Tiago Cardoso
Have you checked MZTools?? It does a lot of cool stuff...
你检查过MZTools 吗?? 它做了很多很酷的事情......
If I'm not wrong, one of the functionalities it offers is to set your own shortcuts.
如果我没记错的话,它提供的功能之一就是设置您自己的快捷方式。
回答by j2associates
Or just click View, ToolBars, Edit. Then you can select a block of code and then click the Commentor Uncommenttoolbar button to do everything in one click.
或者只需单击View、ToolBars、Edit。然后,您可以选择一段代码,然后单击“注释”或“取消注释”工具栏按钮,一键完成所有操作。
As an aside, you can Tab/Shift+Taba 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.
顺便说一句,您也可以Tab/ Shift+Tab一块选定的文本。当我还是个菜鸟时,我很长一段时间都不知道这一点,并且一次只做一行。
Good Luck!
祝你好运!
回答by Rohan Bellimal
Steps to comment / uncommented
评论/取消评论的步骤
Press alt + f11/ Developer tab visual basic editor view tab - toolbar - edit - comments.
按 alt + f11/ Developer tab 可视化基本编辑器视图选项卡 - 工具栏 - 编辑 - 注释。
回答by 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 &Cin the name field and get the same result as &Comment Block (without the lengthy text).
将图标添加到工具栏后并修改所选图标时,名称输入中的与号指定下一个字符是与 Alt 一起用于快捷方式的字符。由于您必须从“修改选择”下拉菜单中选择一个包含显示文本的显示选项,您还可以在名称字段中写入&C并获得与 &Comment Block 相同的结果(没有冗长的文本)。
回答by Aaron Lelevier
An easy way to add buttons to Comment
or Un-Comment
a code block is:
向代码块添加按钮Comment
或Un-Comment
代码块的简单方法是:
- 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.
- 转到查看-工具栏-自定义
- 选择命令选项卡
- 选择左侧的编辑类别
- 将“注释块”和“取消注释块”图标拖到工具栏上。
回答by 6diegodiego9
With MZ-Tools installed, I comment/uncomment blocks in VBE by using the keyboard shortcut
Ctrl+Alt+C(MZ-Tools default)
安装 MZ-Tools 后,我使用键盘快捷键
Ctrl+ Alt+ C(默认 MZ-Tools)在 VBE 中注释/取消注释块