How do I use the .pagesetup properly?

vbaNumpty

Board Regular
Joined
Apr 20, 2021
Messages
171
Office Version
  1. 365
Platform
  1. Windows
I am trying to use different page setups but I get object errors. What is the proper way to use the page setup arguments?

VBA Code:
Sub Print_DB()

    Dim db As Worksheet

    With db
        
        .PageSetup.FitToPagesWide
        .PageSetup.PaperSize = xlPaperLetter
        .PageSetup.Orientation = xlLandscape
        
        
    End With
    
    Range("DBPrint").PrintPreview

End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
First, you've declared db, but you haven't assign it a worksheet. Secondly, you haven't assigned a value to the FitToPagesWide property. Try the following...

VBA Code:
Sub Print_DB()

    Dim db As Worksheet
    Set db = Worksheets("Sheet1") 'change the sheet name accordingly

    With db.PageSetup
        .FitToPagesWide = 1
        .PaperSize = xlPaperLetter
        .Orientation = xlLandscape
    End With
  
    Range("DBPrint").PrintPreview

By the way, you can speed up things a bit by setting the PrintCommunication property to False before your code, and then back to True afterwards...

VBA Code:
Sub Print_DB()

    Dim db As Worksheet
    Set db = Worksheets("Sheet1") 'change the sheet name accordingly

    Application.PrintCommunication = False
    With db.PageSetup
        .FitToPagesWide = 1
        .PaperSize = xlPaperLetter
        .Orientation = xlLandscape
    End With
    Application.PrintCommunication = True
  
    Range("DBPrint").PrintPreview

End Sub

Hope this helps!
 
Upvote 0
Solution
First, you've declared db, but you haven't assign it a worksheet. Secondly, you haven't assigned a value to the FitToPagesWide property. Try the following...

VBA Code:
Sub Print_DB()

    Dim db As Worksheet
    Set db = Worksheets("Sheet1") 'change the sheet name accordingly

    With db.PageSetup
        .FitToPagesWide = 1
        .PaperSize = xlPaperLetter
        .Orientation = xlLandscape
    End With
 
    Range("DBPrint").PrintPreview

By the way, you can speed up things a bit by setting the PrintCommunication property to False before your code, and then back to True afterwards...

VBA Code:
Sub Print_DB()

    Dim db As Worksheet
    Set db = Worksheets("Sheet1") 'change the sheet name accordingly

    Application.PrintCommunication = False
    With db.PageSetup
        .FitToPagesWide = 1
        .PaperSize = xlPaperLetter
        .Orientation = xlLandscape
    End With
    Application.PrintCommunication = True
 
    Range("DBPrint").PrintPreview

End Sub

Hope this helps!
Thanks a lot for explaining it! Appreciate it.
 
Upvote 0

Forum statistics

Threads
1,223,264
Messages
6,171,081
Members
452,377
Latest member
bradfordsam

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