Speeding up a VBA macro for printing headers

robwood83

New Member
Joined
Aug 3, 2010
Messages
5
Hello All,

I have a Macro I am using to apply a custom header to certain sheets in a workbook, only about half the sheets. This is what I am using.
Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
    Worksheets("Sheet1").PageSetup.LeftHeader = "& PETAP# " & Sheets("Turn-In").Range("G28").Value
    Worksheets("Sheet1").PageSetup.RightHeader = Sheets("Turn-In").Range("V28").Value
    Worksheets("Sheet2").PageSetup.LeftHeader = "& PETAP# " & Sheets("Turn-In").Range("G28").Value
    Worksheets("Sheet2").PageSetup.RightHeader = Sheets("Turn-In").Range("V28").Value
    Worksheets("Sheet3").PageSetup.LeftHeader = "& PETAP# " & Sheets("Turn-In").Range("G28").Value
    Worksheets("Sheet3").PageSetup.RightHeader = Sheets("Turn-In").Range("V28").Value
    Worksheets("Sheet4").PageSetup.LeftHeader = "& PETAP# " & Sheets("Turn-In").Range("G28").Value
    Worksheets("Sheet4").PageSetup.RightHeader = Sheets("Turn-In").Range("V28").Value
    Worksheets("Sheet7").PageSetup.LeftHeader = "& PETAP# " & Sheets("Turn-In").Range("G28").Value
    Worksheets("Sheet7").PageSetup.RightHeader = Sheets("Turn-In").Range("V28").Value
    Worksheets("Sheet10").PageSetup.LeftHeader = "& PETAP# " & Sheets("Turn-In").Range("G28").Value
    Worksheets("Sheet10").PageSetup.RightHeader = Sheets("Turn-In").Range("V28").Value
    Worksheets("Sheet12").PageSetup.LeftHeader = "& PETAP# " & Sheets("Turn-In").Range("G28").Value
    Worksheets("Sheet12").PageSetup.RightHeader = Sheets("Turn-In").Range("V28").Value
    Worksheets("Sheet15").PageSetup.LeftHeader = "& PETAP# " & Sheets("Turn-In").Range("G28").Value
    Worksheets("Sheet15").PageSetup.RightHeader = Sheets("Turn-In").Range("V28").Value
    Worksheets("Sheet17").PageSetup.LeftHeader = "& PETAP# " & Sheets("Turn-In").Range("G28").Value
    Worksheets("Sheet17").PageSetup.RightHeader = Sheets("Turn-In").Range("V28").Value
   
End Sub

This is working great but seem to really slow Excel down, is there any other options to accomplish this same thing but without the memory drag?
 

Some videos you may like

Excel Facts

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

p45cal

Well-known Member
Joined
Nov 10, 2009
Messages
4,763
I'm not sure it'll speed it up because thes page setup things can take a while sometimes but:
Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet7", "Sheet10", "Sheet12", "Sheet15", "Sheet17")).Select
Sheets("Sheet1").Activate
With ActiveSheet.PageSetup
  .LeftHeader = "& PETAP# " & Sheets("Turn-In").Range("G28").Value
  .RightHeader = Sheets("Turn-In").Range("V28").Value
End With
End Sub
I don't think you can avoid selecting for these purposes.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,142
Messages
5,599,971
Members
414,353
Latest member
ljhan

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