VBA: Select Sheet for Page Setup

NikoleJay

New Member
Joined
May 9, 2020
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hi, again!

I'm on the last step of my project, and I'm probably overthinking it at this point. I am trying to set up the print area, page layout, and width to fit all columns on one page. This report is ran weekly, and the amount of data/number of rows will vary. My documents has two sheets. The first has the buttons I am going to assign the Macros to, and the second sheet has the data. I uploaded a picture in case it helps.

I found examples but it will not work. I am getting a "Compile error: Invalid or unqualified reference" while highlighting the "Sub" line.

Here is the code I tried using for the page setup:
VBA Code:
Sub DSR_Print()
    Dim lastRow As Long
    lastRow = Range("A:J").SpecialCells(xlCellTypeLastCell).Row
    Sheets("Data").Select
    ActiveSheet.PageSetup
        .Orientation = xlLandscape
        .PrintArea = "$A$:$J$" & lastRow
        .FitToPagesTall = False
        .FitToPagesWide = 1
End Sub
 

Attachments

  • Document Status Report.jpg
    Document Status Report.jpg
    186.4 KB · Views: 8

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try this
VBA Code:
Sub DSR_Print()
  Dim lastRow As Long
  Sheets("Data").Select
  lastRow = Range("A:J").SpecialCells(xlCellTypeLastCell).Row
  With ActiveSheet.PageSetup
    .Orientation = xlLandscape
    .PrintArea = "A1:J" & lastRow
    .FitToPagesTall = False
    .FitToPagesWide = 1
  End With
End Sub
 
Upvote 0
Thank you! Everything works except the print area. The width is correct, but it doesn't end after the "signature" cell. I might have been trying to use the wrong command for that.
 
Upvote 0
Try this:

VBA Code:
Sub DSR_Print()
  Dim lastRow As Long
  Sheets("Data").Select
  lastRow = Range("A" & Rows.Count).End(3).Row
  With ActiveSheet.PageSetup
    .Orientation = xlLandscape
    .PrintArea = "A1:J" & lastRow
    .FitToPagesTall = False
    .FitToPagesWide = 1
    .FitToPagesTall = 1
  End With
End Sub
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,004
Messages
6,122,659
Members
449,091
Latest member
peppernaut

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