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

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
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,215,824
Messages
6,127,092
Members
449,358
Latest member
Snowinx

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