Solution: Dynamically set Print area with VBA

robertwp7472

New member
Joined
Jul 22, 2016
Messages
86
Reaction score
0
Points
0
Location
Fox Island, WA
To anyone who needs it:
Code:
Sub SetPrintArea()  'Posted by Richard Winfield on January 14, 2002 on Mr. Excel 'Merged with Posting by VoG (Mr. Excel MVP) Feb 21st, 2009 'and from RichNH on www.experts-exchange.com  Dim myrange As String 

'Change the 7 to whatever Column Count you require A - Z = 1 - 26 
     myrange = Cells(Rows.Count, 7).End(xlUp).Address    

 With ActiveSheet.PageSetup      
         'Set Print area      
        .PrintArea = "$D$2:" & myrange 'Change $D$2 to whatever the top/left corner of desired area is             

        'Set Margins      
        .LeftMargin = 0      
        .RightMargin = 0      
        .TopMargin = 0      
        .BottomMargin = 0              

        'Set Orientation on paper      
        .CenterHorizontally = True      
        .CenterVertically = False      
        .Orientation = xlLandscape       
        .Zoom = False      
        .FitToPagesWide = 1       
       .FitToPagesWide = False            
 End With  

End Sub

Hope this helps someone.
 
Last edited:
I dunno nothin'

This worked for me perfectly yesterday but today it only selects my original spreadsheet for the print area and not any added rows. Very perplexing.
 
Back
Top