VBA Event?

Misca

Well-known Member
Joined
Aug 12, 2009
Messages
1,749
Office Version
  1. 365
Platform
  1. Windows
I have a workbook template with some VBA code and I'd like to run a macro when new workbooks are created from the template. However, I can't seem to find the New Workbook event (the closest being Open Workbook which doesn't seem to work).

What would be the right event for the new workbook and where can I find it?
 
Last edited:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
There isn't a new workbook event however the workbook open one should work. The code needs to be placed on the ThisWorkbook code page of the VBA project.

Dom
 
Upvote 0
There is a Workbook.New event, but it is a VSTO tool.

You should be able to use the Workbook_Open event. You can test if the opened wb name does not match the template name, i.e. If ThisWorkbook.Name <> "Template.xls", which indicates that it's been opened as a template copy, not the template itself. That will keep your code from firing if you need to modify the template.

Hope that helps,

EDIT: what he said. :)
 
Upvote 0
There is an application level NewWorkbook event.
The way that I access it is through my Personal Macro Workbook.

This is in a Class module named AppLevelEvents
Code:
Public WithEvents thisApp As Application

Private Sub thisApp_NewWorkbook(ByVal Wb As Excel.Workbook)
    With Wb
        .PrecisionAsDisplayed = False
        .Date1904 = False
        .Saved = True
    End With
End Sub
and this in the ThisWorkbook module of the Personal Macro Workbook
Code:
Private Sub Workbook_Open()
    Set ThisApplication = New AppLevelEvents
    Set ThisApplication.thisApp = Application
End Sub
Your code should be substituted for mine in the NewWorkbook event.
 
Upvote 0
Thanks, everybody and mikerickson especially for your help!

I think I can solve the problem with your advice.
 
Upvote 0

Forum statistics

Threads
1,214,981
Messages
6,122,566
Members
449,089
Latest member
Motoracer88

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