Solved

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

Posted on 2016-09-28
7
34 Views
Last Modified: 2016-09-29
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
0
Comment
Question by:Ben Conner
  • 3
  • 3
7 Comments
 
LVL 46

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
ID: 41820808
Select the area that you want to print and then from the Page Layout tab select Print Area and then Set Print Area, After that print preview will no longer show the blank pages.
0
 
LVL 81

Accepted Solution

by:
byundt earned 250 total points
ID: 41820878
In your file, column G is part of the Print Area, but does not fit on the page. As a result, you print two extra pages to get column G, even though it is blank.
0
 

Author Comment

by:Ben Conner
ID: 41820884
Good grief.  Hadn't even seen that.  Thanks much guys!

--Ben
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 81

Expert Comment

by:byundt
ID: 41820956
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".
0
 

Author Comment

by:Ben Conner
ID: 41821691
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
0
 
LVL 81

Expert Comment

by:byundt
ID: 41821777
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

0
 

Author Comment

by:Ben Conner
ID: 41821803
Thanks for providing that!   Dropped that in and took out the code I used.  

Greatly appreciated.

--Ben
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question