VBA set printing area for all sheets in a workbook - takes too long to run

lynnsong986

Board Regular
Joined
May 24, 2014
Messages
146
Hello,

I have the below codes that run really slowly, it loops each sheet and find the dynamic range then set the printing area on each. it works but takes a long time (the workbook has about 130 sheets and it takes almost 30 minutes to loop through!)

Can someone please help me revise it to make it run faster?

Also how can I post codes where it belongs...


Sub SetPrintAreaOnMultipleSheetsForReport()
Dim sh As Worksheet
For Each sh In ActiveWindow.SelectedSheets
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.PrintCommunication = False
sh.Activate
myrange = ActiveSheet.Cells(Rows.Count, 9).End(xlUp).Address
ActiveSheet.PageSetup.PrintArea = "$A$1:" & myrange
ActiveSheet.PageSetup.Orientation = xlPortrait
With ActiveSheet.PageSetup
.FitToPagesWide = 1
.FitToPagesTall = 20
End With
Next sh
Application.PrintCommunication = True
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub

thanks,
Lynn
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Any faster?...

Code:
Sub SetPrintAreaOnMultipleSheetsForReport()
    Dim sh As Worksheet
    For Each sh In ActiveWindow.SelectedSheets
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        Application.PrintCommunication = False

        myrange = sh.Cells(Rows.Count, 9).End(xlUp).Address
        sh.PageSetup.PrintArea = "$A$1:" & myrange
        sh.PageSetup.Orientation = xlPortrait
        With sh.PageSetup
            .FitToPagesWide = 1
            .FitToPagesTall = 20
        End With
    Next sh
    Application.PrintCommunication = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
Thanks very much Mark858, the revised codes run faster - I get it because it doesn't need to activate each sheet any more.

also thanks for showing me the code tags...

Lynn
 
Upvote 0
You're welcome, btw the easiest way these days to add the code tags is to copy your code into the thread, select the code and click the # icon above the window.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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