Create auto open VBA for ALL excel files opened?

Jarvoisier

New Member
Joined
Aug 29, 2011
Messages
14
Thanks in advance for any help on this thread. My question is as follows: Is it possible to create a VBA auto-open macro that will run on ALL excel files, not just one specific file? In my line of work it is necessary to format excel spreadsheets in the same manner. With the hope of streamlining the process of processing files, I would like to use the macro code listed below to automatically run every time any excel file is opened rather than having to manually open the file and then push keystrokes to run the macro.

I have researched on this and other forums and have only been able to automatically run a macro when a specific file opens....yet I have not figured out (or learned for that matter) if it is even feasible to create a macro for what I am requesting.

Many thanks in advance and please see the code below for the macro that I'd like to run automatically. Like I mentioned earlier, I can only get this macro to run on the personal.xlsm file itself, and not on successive excel files opened that I would like to have the macro run on automatically.

Private Sub Workbook_Open()
'
' Jarvis_Header_Footer Macro
' Macro recorded 8/23/2011 by Rob Jarvis
'
'
Dim ws As Worksheet
For Each ws In Worksheets
ws.Visible = True
Next

For Each ws In ActiveWorkbook.Worksheets
ws.Visible = xlSheetVisible
With ws.PageSetup
.LeftHeader = ""
.CenterHeader = "&F"
.RightHeader = ""
.CenterFooter = "&A"
.RightFooter = "Page &P of &N"
.LeftMargin = Application.InchesToPoints(0.25)
.RightMargin = Application.InchesToPoints(0.25)
.TopMargin = Application.InchesToPoints(0.5)
.BottomMargin = Application.InchesToPoints(0.5)
.HeaderMargin = Application.InchesToPoints(0.25)
.FooterMargin = Application.InchesToPoints(0.25)
.PrintHeadings = True
.PrintTitleColumns = ""
End With
Next ws
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
You would need to create a general purpose addin, and use a class module with events to capture the event at the application level

Google should be a able to help, but I am working off my phone atm so can't write an example!
 
Upvote 0
Awesome, thanks for that information...I will do a google search and see what I'm able to find.

If by chance you are in front of a computer later and have time to write an example of what you mentioned it will be greatly appreciated!
 
Upvote 0
So I was able to create the add-in, however I'm not able to get the events that I had setup in the original macro to work inside of the module. Any (additional) ideas?
 
Upvote 0
You could instead create a template with that setup, and use it as the basis for new workbooks.
 
Upvote 0
The code that I originally listed needs to be run on existing excel files, not new ones. The particular macro has to be run on workbooks prior to us running the excels through a processing software. In a perfect world we'd like the add-in to run when excel opens the workbook vs habing to manually open the workbook then pushing keystrokes to run the macro (is what we currently do). We're trying to make things more efficient (key word trying lol).

Any additional ideas appreciated!
 
Upvote 0
Hmm.

In the ThisWorkbook module of an add-in,

Code:
Option Explicit
 
Public WithEvents appXl As Application
 
Private Sub Workbook_Open()
    Set appXl = Application
End Sub
 
Private Sub appXl_WorkbookOpen(ByVal Wb As Workbook)
    myMacro
End Sub
Then put this in a code module of the same add-in:

Code:
Private Sub myMacro()
    Dim wks          As Worksheet
 
    For Each wks In ActiveWorkbook.Worksheets
        wks.Visible = xlSheetVisible
        With wks.PageSetup
            .LeftHeader = ""
            .CenterHeader = "&F"
            .RightHeader = ""
            .CenterFooter = "&A"
            .RightFooter = "Page &P of &N"
            .LeftMargin = Application.InchesToPoints(0.25)
            .RightMargin = Application.InchesToPoints(0.25)
            .TopMargin = Application.InchesToPoints(0.5)
            .BottomMargin = Application.InchesToPoints(0.5)
            .HeaderMargin = Application.InchesToPoints(0.25)
            .FooterMargin = Application.InchesToPoints(0.25)
            .PrintHeadings = True
            .PrintTitleColumns = ""
        End With
    Next wks
End Sub
 
Upvote 0
I made an example using the concept that I linked to earlier.

Insert a class and name it cAOPen.
Code:
Option Explicit

'http://support.microsoft.com/kb/213566
Public WithEvents aOpen As Application

Private Sub aOpen_WorkbookOpen(ByVal Wb As Workbook)
  If ActiveWorkbook.Name <> ThisWorkbook.Name Then MsgBox ActiveWorkbook.Name
End Sub

Replace the MsgBox with the call to your macro.

Insert a Module and add:
Code:
Option Explicit

Dim autoOpen As New cAOpen

Sub SetAutoOpen()
  Set autoOpen.aOpen = Application
End Sub

Sub UnSetAutoOpen()
  Set autoOpen.aOpen = Nothing
End Sub

Obviously, you would add your Module level code to this Module or another.

In the ThisWorkbook object:
Code:
Private Sub Workbook_Open()
  SetAutoOpen
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,848
Members
452,948
Latest member
UsmanAli786

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