简体   繁体   English

打开工作簿时运行宏

[英]run macro when open workbook

I have done a search. 我已经搜索了。 The most relevant suggests the macro name of "workbook_open" But I still must manually invoke the module. 最相关的建议是宏名称“ workbook_open”,但是我仍然必须手动调用该模块。 Here's what I've coded. 这是我编写的代码。 (Any other suggestions welcome as this is my first vba script -- at age 73) (欢迎提出任何其他建议,因为这是我的第一个vba脚本-73岁)

Sub Workbook_Open()  
  Dim lastRow As Long     'last row with data  
  Dim thisDate As Double  'start timestamp  
  thisDate = Now()  
  With Sheets("Pressure Log")  
    lastRow = .Range("B" & .Rows.Count).End(xlUp).Row 'populate next row with date/time  
    Range("B" & lastRow).Offset(1) = Format(thisDate, "dddd")  
    Range("B" & lastRow).Offset(1, 1) = Format(thisDate, "mm/dd/yyyy")  
    Range("B" & lastRow).Offset(1, 2) = Format(thisDate, "hh:mm AM/PM")  
    Range("B" & lastRow).Offset(1, 3).Select 'position for user data  
  End With  
End Sub  

In the Visual Basic Editor (VBE), bring up the Project Explorer (Ctrl+R), then double-click the ThisWorkbook module (or right-click it and select "View Code"): 在Visual Basic编辑器(VBE)中,打开项目资源管理器 (Ctrl + R),然后双击ThisWorkbook模块(或右键单击它并选择“查看代码”):

VBE的项目浏览器中的ThisWorkbook

That will bring up the ThisWorkbook module's code-behind . 这将显示ThisWorkbook模块的代码背后 ThisWorkbook represents the workbook that's hosting your VBA project; ThisWorkbook代表托管VBA项目的工作簿; it's a special type of module that inherits all the members of the Excel.Workbook class, which can represent any Excel workbook. 这是一种特殊的模块类型,它继承了Excel.Workbook类的所有成员,该类可以表示任何 Excel工作簿。

At the top of the code pane, you will notice two dropdowns: 在代码窗格的顶部,您会注意到两个下拉列表:

代码窗格

Select Workbook from the left-hand dropdown; 从左侧的下拉菜单中选择Workbook the VBE generates an event handler procedure for the Open event, automatically: VBE自动为Open事件生成事件处理程序过程:

Workbook_Open()过程已创建

Notice the right-hand dropdown now says Open - if you click that dropdown, you'll find that it lists every event that a Workbook can handle; 请注意,右侧的下拉菜单现在显示“ Open -如果单击该下拉列表,您会发现它列出了Workbook可以处理的每个事件 selecting one will automatically generate a method with the correct signature/prototype for it. 选择一个将自动生成具有正确签名/原型的方法。

Now take your code and put it in that event handler procedure, save - and you're set! 现在,将您的代码放入事件处理程序中,保存-一切就绪! Next time that workbook is opened with macros enabled, that event handler will be invoked, and your macro will run. 下次在启用宏的情况下打开该工作簿时,将调用该事件处理程序,并且您的宏将运行。

Have fun! 玩得开心! It's never too late to learn! 学习永远不会太晚!

Adding to Mathieu's answer, there's also one more way to run code upon opening workbook: you can create Auto_Open procedure in standard module. 除了Mathieu的答案外,还有另一种在打开工作簿时运行代码的方式:您可以在标准模块中创建Auto_Open过程。 Warning! 警告! This feature can be removed in next versions of Office, however it's still alive! 可以在下一版本的Office中删除此功能,但是它仍然有效! 😉 😉

In standard module: 在标准模块中:

Sub Auto_Open()
    ' Your code
End Sub

In the same way you can use Auto_Close procedure instead of BeforeClose event handler. 同样,您可以使用Auto_Close过程而不是BeforeClose事件处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM