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?
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

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.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,951
Messages
5,834,545
Members
430,295
Latest member
amdis

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