vba 如何即时禁用和启用宏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7579861/
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 do I disable and enable macros on the fly?
提问by Johan
I would like to test an Excel VBA app I made.
However the VBA code messes around with the visibility of cells and that's a pest when editing the sheet.
我想测试我制作的 Excel VBA 应用程序。
然而,VBA 代码混淆了单元格的可见性,这在编辑工作表时是一个害虫。
Is there an option is enable and disable macro's on the fly without having to
是否有一个选项可以即时启用和禁用宏,而不必
Close the sheet
Change the macro settings
Reopen the sheet
Close the sheet
Change the macro settings.
etc.
回答by JMax
As far as I know, you can't enable / disable macros from an opened workbook on the fly.
Yet, you shouldn't have to because macros are only triggered thanks to a user click.
据我所知,你不能启用从打开的工作簿/禁用宏对飞。
但是,您不必这样做,因为宏仅在用户单击时触发。
The only case I would see is for the Event Procedures (Worksheet_Change
or else).
You could then create procedures to activate / deactivate events and call them from buttons in your worksbook:
我会看到的唯一情况是事件程序(Worksheet_Change
或其他)。
然后,您可以创建过程来激活/停用事件并从工作簿中的按钮调用它们:
Sub enableEvents()
Application.EnableEvents = True
End Sub
Sub disableEvents()
Application.EnableEvents = False
End Sub
You can also try these tips from Chris Pearson websiteusing global vars you would change depending on your needs:
您还可以尝试使用Chris Pearson 网站上的这些提示,使用您可以根据需要更改的全局变量:
Public AbortChangeEvent As Boolean
And check if afterwards:
然后检查是否:
Private Sub Worksheet_Change(ByVal Target As Range)
If AbortChangeEvent = True Then
Exit Sub
End If
'
' rest of code here
'
End Sub
回答by JohannesGbg
You can also hold down SHIFTwhen you open a document to disable macros.
您也可以SHIFT在打开文档时按住以禁用宏。
回答by T.J.L.
To disable macros on the fly, use "Application.EnableEvents = False" via the Immediate window in the VBA editor (and "Application.EnableEvents = True" to turn them back on).
要即时禁用宏,请通过 VBA 编辑器中的立即窗口使用“Application.EnableEvents = False”(并使用“Application.EnableEvents = True”将它们重新打开)。
回答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.
*也许更早的版本。
回答by Shannon C
I have Office 2013 and I found a button on the VBA window that did this for me very easily.
我有 Office 2013,我在 VBA 窗口上找到了一个按钮,可以很容易地为我完成此操作。
There's a play, pause and stop button on the toolbar. In Excel, they are actually called, Run Macro, Break and Reset.
工具栏上有播放、暂停和停止按钮。在 Excel 中,它们实际上被称为 Run Macro、Break 和 Reset。
Click the Break button (pause) and any running macros should stop running. I only tested it on one macro but seems reasonable to presume that this will work in general.
单击“中断”按钮(暂停),任何正在运行的宏都应停止运行。我只在一个宏上对其进行了测试,但似乎可以合理地假设这在一般情况下会起作用。
Also, I believe those buttons were there for many versions of Excel, so it's worth checking earlier versions.
此外,我相信这些按钮适用于许多版本的 Excel,因此值得检查早期版本。