[英]Running vba code whenever a workbook is opened
I'm writing vba which manipulates data within a worksheet however I'm trying to make it run whenever a workbook is opened. 我正在编写vba来操纵工作表中的数据,但是我试图使其在打开工作簿时运行。
The problem I'm having is that due to the workbook (that the code needs to run on) is different/new every time, I need the auto_open code to be within a personal macro workbook. 我遇到的问题是,由于工作簿(需要在其上运行代码)每次都是不同的/新的,因此我需要将auto_open代码放入个人宏工作簿中。
Sub Auto_Open()
Dim bookname As String
Dim checkbook As String
Dim Workbook As Workbook
For Each Workbook In Application.Workbooks
bookname = Workbook.Name
checkbook = Left(bookname, 3)
If checkbook = "EDN" Then
Data_generator
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
Application.Quit
Else
End If
Next Workbook
End Sub
When this code runs it checks all open workbooks and sees if the first 3 letters of it are 'EDN', if it is then run the public sub called 'Data_generator', save it and quit. 运行此代码时,它将检查所有打开的工作簿,并查看它的前3个字母是否为“ EDN”,然后运行名为“ Data_generator”的公共子程序,保存并退出。 If it isn't check the next open workbook, etc. 如果不是,请检查下一个打开的工作簿,等等。
When a file is opened from windows explorer, excel launches (with both the desired workbook and the personal macro workbook) however because excel opens the personal macro workbook first and runs the code before opening the desired workbook it doesn't find a workbook called 'EDN'. 从Windows资源管理器打开文件时,Excel会启动(同时包含所需的工作簿和个人宏工作簿),因为excel首先打开个人宏工作簿并在打开所需的工作簿之前运行代码,所以找不到名为“ EDN”。
If the above code is ran after both workbooks have opened then the code works as intended and cycles through each open workbook to see if there's one called 'EDN' (this was proved by putting a messagebox after the 'then' and running the code), if so run the sub. 如果在两个工作簿都打开后运行了上面的代码,则该代码将按预期工作,并在每个打开的工作簿中循环以查看是否有一个名为“ EDN”的文件(这是通过在“ then”之后放置一个消息框并运行代码来证明的) ,如果是这样,请运行该子程序。
I've proved this by putting a messagebox after the 'else', when this is done it displays the messagebox with the workbook I want, not open. 我已经通过在“ else”之后放置一个消息框来证明了这一点,完成此操作后,它将显示该消息框以及我想要的工作簿,而不是打开的。 After the message box is cleared, the workbook then opens. 清除消息框后,然后打开工作簿。
Is there any way to make the desired workbook open first or any other work around for this? 有什么方法可以使所需的工作簿首先打开,或者是否可以进行其他任何处理?
You can create an Add-in that runs whenever a workbook is open 您可以创建一个在工作簿打开时运行的加载项
https://msdn.microsoft.com/en-us/library/office/gg597509(v=office.14).aspx https://msdn.microsoft.com/en-us/library/office/gg597509(v=office.14).aspx
this tool may help to create the Add in http://www.andypope.info/vba/ribboneditor.htm 此工具可能有助于在http://www.andypope.info/vba/ribboneditor.htm中创建“添加”
You should be able to use the Application.OnWindow event to trigger a macro when a file is opened or closed. 您应该能够使用Application.OnWindow事件在打开或关闭文件时触发宏。
In ThisWorkbook 在本工作簿中
Private Sub Workbook_Open()
Call StartTracking
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call StopTracking
End Sub
In a Module 在模块中
Function StartTracking()
Application.OnWindow = "AutoRunOnWindowChange"
End Function
Function StopTracking()
Application.OnWindow = ""
End Function
Function AutoRunOnWindowChange()
If Left(ActiveWorkbook.Name, 3) = "EDN" Then
Call Data_generator
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
Application.Quit
End If
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.