vba 打开时自动运行宏的好方法是什么

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

what is a good way to autorun macros upon open

excelvbaautomationautorun

提问by crazian

I've tried using the code that many sites have suggested to autorun a list of macros upon opening an Excel workbook. Attached is my VBA code on ThisWorkbook:

我尝试使用许多网站建议在打开 Excel 工作簿时自动运行宏列表的代码。附件是我在 ThisWorkbook 上的 VBA 代码:

Private Sub WorkbookOpen()


MsgBox "STOP!  Do NOT attempt to highlight any fields manually!" & vbCrLf & _
     "Any highlighting will be overwritten upon reentry of this workbook.", vbOKOnly     +vbExclamation

Call Melanoma.ReformatDeplete
Call Melanoma.CScheckNO
Call Melanoma.CScheckMissing
Call Glioma.ReformatDeplete
Call Glioma.ReformatGBM
Call Glioma.CScheckNO
Call Glioma.CScheckMissing
Call Breast.ReformatDeplete
Call Breast.CScheckNO
Call Breast.CScheckMissing
Call Lymphoma.ReformatDeplete
Call Lymphoma.CScheckNO
Call Lymphoma.CScheckMissing
Call Lung.ReformatDeplete
Call Lung.CScheckNO
Call Lung.CScheckMissing
Call Miscellaneous.ReformatDeplete
Call Miscellaneous.CScheckNO
Call Miscellaneous.CScheckMissing
Call Normals.ReformatDeplete
Call Normals.CScheckNO
Call Normals.CScheckMissing


End Sub

Obviously, I saved the workbook as a 2010 macro-enabled workbook, but when I open the workbook, nothing happens on its on, I still have to click the "run button" in VBA

显然,我将工作簿保存为 2010 启用宏的工作簿,但是当我打开工作簿时,它没有任何反应,我仍然需要单击 VBA 中的“运行按钮”

Any suggestions?

有什么建议?

Thanks!

谢谢!

回答by Portland Runner

You are close, just add the underscore

你很接近,只需添加下划线

Private Sub Workbook_Open()

回答by Siddharth Rout

There are two main ways to run the macro on workbook open

在工作簿打开时运行宏有两种主要方法

  1. Which Portland Runner has already mentioned in his post. Private Sub Workbook_Open()

  2. Use Sub Auto_Open()in a module. While we are at it, you may also want to check THIS. The link is about Configure a macro to run automatically upon opening a workbook

  1. 波特兰赛跑者在他的帖子中已经提到过。 Private Sub Workbook_Open()

  2. Sub Auto_Open()在模块中使用。当我们在做的时候,你可能还想检查一下THIS。链接是关于Configure a macro to run automatically upon opening a workbook

Private Sub Workbook_Open()is better than Sub Auto_Open()because of many reasons as mentioned in that link.

Private Sub Workbook_Open()Sub Auto_Open()由于该链接中提到的许多原因要好。

回答by Gary's Student

There are two possible reasons the macro will not run automatically:

宏不会自动运行有两个可能的原因:

  1. security settings
  2. the macro is not located in the workbook code area
  1. 安全设定
  2. 宏不在工作簿代码区