简体   繁体   中英

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.

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.

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. 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'.

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.

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. 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

this tool may help to create the Add in 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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