execute macro when opening any workbook

dsgs

Active Member
Joined
Oct 25, 2004
Messages
360
hi all,

i'm trying to execute a macro (with will adjust the footer) and it needs to run everytime a workbook is opened.

I allready have an add-in with some macro's so i will add it to the add-in

Code:
With ActiveSheet.PageSetup
        .LeftFooter = "&Z&F" & Chr(10) & "&D"
        .BottomMargin = Application.InchesToPoints(0.75)
        .FooterMargin = Application.InchesToPoints(0.4)
End With

this needs to be triggered on opening any workbook for the opened workbook....

is this possible?

thanx a lot!

david
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
unfortunately not everything works...

when i used my macro it only changes the active worksheet but i want my footer on all pages!

Code:
Private Sub App_NewWorkbook(ByVal wb As Workbook)
Dim ws As Worksheet
    
    For Each ws In ThisWorkbook.Worksheets
    With ActiveSheet.PageSetup
        .LeftFooter = "&Z&F" & Chr(10) & "&D"
        .BottomMargin = Application.InchesToPoints(0.75)
        .FooterMargin = Application.InchesToPoints(0.4)
    End With
    Next ws
End Sub

this doesn't work.....

any suggestions?
 
Upvote 0
That's because you are using ThisWorkbook (the one containing your code) rather than wb (the new Workbook); and because you are using ActiveSheet rather than ws (the next one in the collection).

Try the untested:

Code:
Private Sub App_NewWorkbook(ByVal wb As Workbook) 
    Dim ws As Worksheet 
    For Each ws In wb.Worksheets 
    With ws.PageSetup 
        .LeftFooter = "&Z&F" & Chr(10) & "&D" 
        .BottomMargin = Application.InchesToPoints(0.75) 
        .FooterMargin = Application.InchesToPoints(0.4) 
    End With 
    Next ws 
End Sub
 
Upvote 0

Forum statistics

Threads
1,217,356
Messages
6,136,078
Members
449,988
Latest member
Mabbas

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