3

I want a Sub in my PERSONAL.XLAM file to run every time any workbook is opened. The following works, but only when opening a workbook when no other workbook is open.

Private Sub Workbook_Open()
MsgBox "Hello."
End Sub

How can I make this work upon opening additional workbooks?

7
  • you could write a code in the Auto_Open sub of each workbook in question maybe ? I am not sure I fully understand your question... and to make sure you don't try to open it more than once you can verify if it is open or not before doing so Commented May 12, 2017 at 14:36
  • I want a macro to automatically run on every single workbook that is opened as soon as it is opened. There are going to be thousands of different workbooks, so I can't add them to each workbook. Commented May 12, 2017 at 14:38
  • Do you open those workbooks via a Main workbook ? Commented May 12, 2017 at 14:39
  • 1
    You can't:( Personal.xlam (or any other file) can't see when other files are opened. It works for you for the first opening only because that time personal.xlam is opened too. The only way is to place a code in all of your workbooks. Commented May 12, 2017 at 14:44
  • 4
    cpearson.com/excel/AppEvent.aspx Commented May 12, 2017 at 14:47

1 Answer 1

5

As per the comment from Masoud, this is a great article. You can find all the detail there, but simply, this is all you need:

Private WithEvents App As Application

Private Sub Workbook_Open()
    Set App = Application
End Sub

Private Sub App_NewWorkbook(ByVal Wb As Workbook)
    MsgBox "New Workbook: " & Wb.Name
End Sub

Place the code into your ThisWorkbook module.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.