vba 如何禁用/重新启用 Excel 中的宏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13807651/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:45:38  来源:igfitidea点击:

How to disable/re-enable macros in Excel?

excelvbaexcel-vbavisual-studio-macros

提问by ravemir

I have been using a macro on an excel document that zooms in charts present in any sheet. The problem is, if I want to edit a chart, I must click it, triggering the zoom.

我一直在 Excel 文档上使用宏来放大任何工作表中的图表。问题是,如果我想编辑图表,我必须点击它,触发缩放。

For this particular reason, I need to disable macros, and was wondering if there is either an easy to reach button or a keyboard shortcut to toggle it.

出于这个特殊原因,我需要禁用宏,并且想知道是否有易于访问的按钮或键盘快捷键来切换它。

回答by Scott Holtzman

In the XL file:

在 XL 文件中:

1) Press Alt+F11

1) 按 Alt+F11

2) Go to the ThisWorkbook module and comment out the two lines inside both subs.

2) 转到 ThisWorkbook 模块并注释掉两个 subs 中的两行。

3) Save and close the file, then reopen.

3) 保存并关闭文件,然后重新打开。

4) You can then edit to your hearts content.

4) 然后你可以编辑你的心内容。

5) To put the zoomer back on, uncomment the lines. You may need to Save, Close and Reopen again

5) 要重新打开缩放器,请取消注释这些行。您可能需要再次保存、关闭和重新打开

NB-> see comments on original post for more information.

注意-> 查看原始帖子的评论以获取更多信息。

回答by David Zemens

If you'd like a little more control without having to constantly comment/uncomment the macro, you might add something like this to the macro. Make sure you put it at the top, before any of the other code:

如果您想要更多的控制而不必经常注释/取消注释宏,您可以向宏添加类似这样的内容。确保将它放在顶部,在任何其他代码之前:

Dim mb As VbMsgBoxResult    'Declare a variable to hold the messagebox result
'Prompt the user:
mb = MsgBox("Would you like to edit this chart?", vbYesNo, "Edit Chart?")
If mb = vbYes Then
    Exit Sub   'If user wants to edit the chart, then exit this subroutine
    'a response of "No" will allow the sub to continue uninterrupted
End If

Without seeing exactly how the macro is being called, this might require some tweaking, but it's essentially just a prompt asking whether you want the macro (zoom) to continue, or whether you want it to terminate, allowing you to access the chart object for editing.

没有看到宏是如何被调用的,这可能需要一些调整,但它本质上只是一个提示,询问您是否希望宏(缩放)继续,或者是否希望它终止,允许您访问图表对象编辑。

回答by Panos Gr

As of Excel 2010* there's actually an option on the "Developer" tab which allows you to disable macros as easy as ABC. Design Mode lets you do just that!

从 Excel 2010* 开始,“开发人员”选项卡上实际上有一个选项,可让您像 ABC 一样轻松地禁用宏。设计模式可让您做到这一点!

*Maybe even earlier versions.

*也许更早的版本。