Vba Blank Pages After Print Area

kmmsquared

New Member
Joined
Jan 7, 2011
Messages
33
Hi,
I am using the following code to set print ranges. I use this same exact code as part of another macro and it formats the pages correctly (it only uses the number of pages tall that is necessary). In a separate workbook, the columns format correctly in width, but there are extra pages added after the last row of data. For example when the spreadsheet print range should be 55 pages long, output is 290 pages--just blank pages (except for the repeated row 1) after page 55.

How can I fix this? Any help would be appreciated! Thanks!

Code:
Sub PageSetup()
 
Dim LastRow As Long
Dim LastColumn As Long
With Sheets("B")
   LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
   LastColumn = Application.WorksheetFunction.Match("E", Range("1:1"), 0)
 
Sheets("B").Select
Sheets("B").Activate
With ActiveSheet.PageSetup
.FirstPageNumber = 1
.PrintTitleRows = "$1:$1"
.PaperSize = xlPaperLegal
.Orientation = xlLandscape
.Zoom = False
.View = xlPageBreakPreview
[COLOR=red].FitToPagesWide = 1
.FitToPagesTall = 1000
[/COLOR].PrintArea = "A1:" & LastColumn & LastRow
 
End With
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
As written, your code will fail since it is missing an "End With" statement.

Your "LastColumn" variable is being set using the column number where "E" is found in the first row.
Does the workbook where this adds too many pages have an "E" in row 1?
 
Upvote 0
then you're probably suffering from the fact you've already told Excel the file is 1000 pages tall, or something like that. Its a common Excel problem, that it doesn't reset the last cell in the sheet once it has been used

As a one-off, delete all rows from your last row to Excel's last row, then select cell A1, and save the file. You'll notice the vertical scroll bar should change size during file save

If this issue is being generated by your other code, then you'll need to reset the sheet size programattically too. Search for reducing file sizes, resetting last cell, something like that
 
Upvote 0
@Datsmart--there is a "With End" statement, I just forgot to copy it over. :oops:

There is an "E" in row 1.

@baitmaster

I may have to try your solution...nothing else is working!
 
Upvote 0
I reset the last cell and finally got it to work. Thanks!!!! :)
Would you mind if I asked what you added to the code to fix this? I have the same issue......I have three pages that I want to print to pdf which is good on the excel sheet, however, when it prints, I get an extra 5 blank pages, and do not know what to put in the code to stop it. Thank you.
 
Upvote 0

Forum statistics

Threads
1,217,394
Messages
6,136,365
Members
450,006
Latest member
DaveLlew

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