VBA - Setting Footers on Opening Workbook

mickcl

New Member
Joined
Jan 1, 2007
Messages
4
I am looking to set the Footer for all worksheets on opening of workbook.

Code:
Private Sub Workbook_Open()
    For Each ws In ActiveWorkbook.Worksheets
        With ws.PageSetup
            .LeftFooter = " 2 LEFT"
            .CenterFooter = "2 CENTRE"
            .RightFooter = "2 RIGHT"
           End With
   Next
End Sub

Using the code above I get the error 'Objective variable or with block variable not set'. Is this the best way or can it be achieved another way.

Thanks
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi,

Does this clear your error...

Code:
Private Sub Workbook_Open()
[COLOR=#ff0000]    Dim ws As Worksheet[/COLOR]
    For Each ws In ActiveWorkbook.Worksheets
        With ws.PageSetup
            .LeftFooter = " 2 LEFT"
            .CenterFooter = "2 CENTRE"
            .RightFooter = "2 RIGHT"
           End With
   Next
End Sub
 
Upvote 0
I don't know what to say, I ran it both ways, got the error with your code, added the declaration and it ran fine. I even double checked in Print Preview and the footers were present on every sheet.
 
Upvote 0
Hi,

I am continuing to work out what is happening.

FYI, In any case when I debug the error it always seem to error on this line: For Each ws In ActiveWorkbook.Worksheets

Regards,
 
Upvote 0
And the same error message... "Objective variable or with block variable not set"?

I just copied and pasted my code in Post #2 into a new workbook and it ran fine. Perhaps someone with more coding chops will untangle this for us.

Regards,






 
Upvote 0
Hi,

I am concerned that the reason maybe related to the code implementation.

The code is within the personal.xlsm and I wouold like it to run as you open any Workbook to set the Footer.

Sorry, about not mentioning this as I am new to Excel VBA.

Regards,
 
Upvote 0
I am not savvy enough to know if the implementation is the problem.

Why don't you try writing it into a single workbook and see if that fixes your problem. It's a quick copy and paste to try and problem solve.

Regards,
 
Upvote 0

Forum statistics

Threads
1,214,424
Messages
6,119,404
Members
448,893
Latest member
AtariBaby

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