Link to home
Start Free TrialLog in
Avatar of Ben Conner
Ben ConnerFlag for United States of America

asked on

Print Preview in Excel 2010 generating 2 blank pages at end of document

Hi,

I have a spreadsheet I create from a subset of another one with a VBA routine.  The spreadsheet is getting 2 extra pages at the end of it in print preview mode that I can't explain.  Anyone have any suggestions as to what could be causing this?

The VBA code I've used in the past to mitigate this is:

    ' The following forces a recalculation of what the actual used range is for this
    ' tab.  Sometimes Excel prints blank pages; this fixes that.
    a = ActiveSheet.UsedRange.Rows.Select
   
    With ActiveSheet.PageSetup
      .Orientation = xlLandscape
      .PaperSize = xlPaperLetter
      .PrintTitleRows = "$20:$20"
      .PrintTitleColumns = ""
      .Zoom = 100
      .FitToPagesWide = 1
      '.FitToPagesTall = 1
      .LeftFooter = "&""Calibri""&08" & "Centers for Medicare and Medicaid Services" & Chr(10) & _
                    "FMQAI - Hospital Outpatient Quality Reporting Program Support Contractor"
      .RightFooter = "&""Calibri""&08 Page &P of &N "
   
    End With

But that doesn't seem to be working in this instance.  

Thanks!

--Ben
000001_Hospital-OQR-Program-Claims-.xlsx
SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ben Conner

ASKER

Good grief.  Hadn't even seen that.  Thanks much guys!

--Ben
Immediately after opening the file, I checked the value of ActiveSheet.UsedRange.Address, and found it was $A$1:$G$50. A print preview showed four pages. I thereupon deleted column G to reset the used range to $A$1:$F$50. After doing so, the printout took only two pages.

Next, I opened another copy of the file and deleted column G. Print preview annoyingly still showed four pages. But when I calculated the used range in VBA, it displayed the correct answer and the print preview subsequently showed only two pages.

Conclusion: you must execute a VBA statement that references the UsedRange property of the worksheet to actually reset it.

I also found an irritatingly sticky Custom Paper Size setting in the Page Setup dialog. I could not change it at all. The statement trying to change it also errored out in the macro posted in the question body. To overcome this problem, I used File...Print and then clicked on the Printer Properties link, and changed the Media type from "Unspecified" to "Plain".
Yes, I see that now...when I had the code in the vba:
    a = ActiveSheet.UsedRange.Rows.Select

it pulled in column G.  Is there a different approach to calculating the used range I should try?

I did beat it into submission by adding:
       ActiveWindow.View = xlPageBreakPreview
       ActiveSheet.PageSetup.PrintArea = "$A:$F"

but I don't consider that to be optimal.  Wish I knew why it chose a blank column as part of the spreadsheet.

--Ben
Ben,
When resetting the used range, I prefer to count the number of rows or columns and assign the value to a variable. Selecting a range is unnecessary.

For your particular problem, the following code may be used to reset the used range so printing needs only two pages. It deletes columns from the right until there are one or more data in the rightmost column in the used range.

Brad

Sub ResetUsedRangeColumns()
Dim i As Long, nCols As Long
Dim rg As Range
With ActiveSheet
    Do
        Set rg = .UsedRange
        nCols = rg.Columns.Count
        If Application.CountA(rg.Columns(nCols)) > 0 Then
            Exit Do
        Else
            rg.Columns(nCols).EntireColumn.Delete
        End If
        nCols = .UsedRange.Columns.Count
        i = i + 1
        If i > 100 Then Exit Do
    Loop
End With
End Sub

Open in new window

Thanks for providing that!   Dropped that in and took out the code I used.  

Greatly appreciated.

--Ben