mez*_*hic 18 excel vba excel-vba
我正在使用VBA打开电子表格,并且一些工作簿包含在调用Workbook_Open()时开始执行的代码.
如何使用VBA打开工作簿但停止代码自动执行?我只打开工作簿来查看工作表中的公式 - 我不希望任何代码执行.
小智 23
是否要在VBA中打开工作簿之前尝试禁用事件,然后为模块的其余部分重新启用它们?尝试使用这样的东西:
Application.EnableEvents = False 'disable Events
workbooks.Open "WORKBOOKPATH" 'open workbook in question
Application.EnableEvents = True 'enable Events
Run Code Online (Sandbox Code Playgroud)
Rau*_*mas 14
我不知道为什么在其他答案中没有明确提到这一点,但我发现Application.AutomationSecurity它完全符合要求.基本上
Application.AutomationSecurity = msoAutomationSecurityByUI
'This is the default behavior where each time it would ask me whether I want to enable or disable macros
Application.AutomationSecurity = msoAutomationSecurityForceDisable
'This would disable all macros in newly opened files
Application.AutomationSecurity = msoAutomationSecurityLow
'This would enable all macros in newly opened files
Run Code Online (Sandbox Code Playgroud)
即使在代码运行后,设置也不会恢复为默认行为,因此您需要再次更改它.因此对于这个问题
previousSecurity = Application.AutomationSecurity
Application.AutomationSecurity = msoAutomationSecurityForceDisable
' Your code
Application.AutomationSecurity = previousSecurity
Run Code Online (Sandbox Code Playgroud)
这里有另一种打开vba的方法
Start Excel Application > Go to File > Recent >
按住Shift键并双击打开 -
这样做可以防止Workbook_Open事件触发和Auto_Open宏运行.
或按住Shift键并双击打开工作簿.