VBA 在文件启动时运行/执行某些操作

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

VBA Run / do something when the file start up

vbaexcel-vbaexcel-2003excel

提问by George

As an alternative to global variable, I am trying to read the last value I worked on at start up. I read this: Excel VBA: Workbook_Open, and this. and tried to implement my own. But it is not doing anything at the moment.

作为全局变量的替代方案,我试图读取我在启动时处理的最后一个值。我读了这个:Excel VBA: Workbook_Open这个. 并试图实现我自己的。但它目前没有做任何事情。

Private Sub Workbook_Open()

    MsgBox "Testing.."

End Sub

I noticed when I paste the following code into my project, it is in the general, instead of in the worksheet. I don't know if that's the problem, I already have a method that listen to changed in my worksheet.

我注意到当我将以下代码粘贴到我的项目中时,它generalworksheet. 我不知道这是否是问题所在,我已经有一个方法可以在我的worksheet.

Edit: This is the screen I am at right now... enter image description here

编辑:这是我现在所在的屏幕... 在此处输入图片说明

Edit: I see what happened. ****I must open view > Project Exloper > This Workbook **** in order to click on ThisWorkbook. Thank you for your help!

编辑:我明白发生了什么。****我必须打开视图 > 项目资源管理器 > 此工作簿**** 才能单击ThisWorkbook. 感谢您的帮助!

回答by Bathsheba

I think you've probably put this code in the wrong module.

我想你可能把这段代码放在了错误的模块中。

It needs to go in "ThisWorkbook" section of the project.

它需要进入项目的“ThisWorkbook”部分。

Just double click on "ThisWorkbook" in the Project explorer window in the VBA editor and paste your code into that.

只需在 VBA 编辑器的项目资源管理器窗口中双击“ThisWorkbook”,然后将您的代码粘贴到其中。