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

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

dsgs

Active Member
Joined
Oct 25, 2004
Messages
360
thx!

Hi Andrew!

still helping us out!!!
it works great!

thanx

david
 

dsgs

Active Member
Joined
Oct 25, 2004
Messages
360
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?
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
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
 

Forum statistics

Threads
1,136,990
Messages
5,678,989
Members
419,797
Latest member
ikethegenius

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
Top