VBA: The Efficiency of a Piece of Code

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,406
According to a client of mine, the following code takes about 20 seconds to run on his computer. However, it only takes about 1.6 seconds to run on my Windows based computer, and about 3 seconds on my old Mac computer. Even with what he says is a slow computer, I don't see why it would take so long. Here's the code...

Code:
Sub UpdateHeader()

    Dim i As Long
     
    ActiveSheet.PageSetup.CenterHeader = Range("q10").Value
    
    With Worksheets(1).PageSetup
         
        For i = 2 To 6
            Worksheets(i).PageSetup.LeftHeader = .LeftHeader
            Worksheets(i).PageSetup.CenterHeader = .CenterHeader
            Worksheets(i).PageSetup.RightHeader = .RightHeader
            Worksheets(i).PageSetup.LeftFooter = .LeftFooter
            Worksheets(i).PageSetup.CenterFooter = .CenterFooter
            Worksheets(i).PageSetup.RightFooter = .RightFooter
        Next i
        
    End With


End Sub

Does any one have any insight, comments, etc.?
 

Some videos you may like

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
This may be a printer driver issue. My understanding is that any code that changes printing properties interacts with the printer driver.
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
P.S. In Excel 2010 you can use

Code:
Application.PrintCommunication = False
 

Firefly2012

Well-known Member
Joined
Dec 28, 2011
Messages
3,638

ADVERTISEMENT

You may find using ExecueExcel4macro commands speeads things up too 9once you've got the settings you want to apply to each sheet). I haven't used this before but i know this is one common way to speed up page setup as it doesn#'t involve communicating with the printer. There seems to be a thread on this here:

http://www.mrexcel.com/forum/showthread.php?362900-Using-XLM4-Page-Setup
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,406
VoG, shg, Firefly2012

Thank you very much for your help. I wasn't aware of this issue. So it's definitely good to know. I'll post back with the results.

Thanks again!
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,406
For compatibility reasons, the XL4 macro was used. Apparently, it was "noticeably faster". :)

Thanks again for everyone's help!

Cheers!
 

Watch MrExcel Video

Forum statistics

Threads
1,123,535
Messages
5,602,217
Members
414,513
Latest member
junbuggle

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