Making macro run when any workbook is opened

awsumchillicrab

Board Regular
Joined
Jan 30, 2011
Messages
56
I'm trying to make a macro run whenever any workbook is opened, regardless of whether Excel is open already. This is what I have in my PERSONAL.XLSM vba project:

Class1:
Option Explicit

Public WithEvents ExcelApp As Application

Private Sub ExcelApp_NewWorkbook(ByVal Wb As Workbook)
MsgBox "Brand new workbook opened"
End Sub

Private Sub ExcelApp_WorkbookOpen(ByVal Wb As Workbook)
MsgBox "Existing workbook opened"
End Sub

ThisWorkbook:
Option Explicit

Private Sub Workbook_Open()
Dim myobject As New Class1
MsgBox "The Personal.xlsm workbook is opened, auto macro activated!"
Set myobject.ExcelApp = Application
End Sub


This doesn't work at all as subsequent workbooks I open don't show the Msgbox "Brand new workbook opened" or "Existing workbook opened". I'm guessing it's because the "Set myobject.ExcelApp = Application" has to be contained in a Module instead of ThisWorkbook. If that's true, how do I get code in a Module to automatically run?

And if it's not true, then how do I resolve this?
 
woohoo!

That solved it shg! Thanks to both MVPs!

Mike,
Leaving it in the Open Event still doesn't work. It's similar to the initial problem I had when posting. Anyhow, not to complain, just wanted to feedback since you took time to help me out.
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Forum statistics

Threads
1,224,586
Messages
6,179,718
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top