Written by Allen Wyatt (last updated November 21, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
Michelle has a lengthy worksheet that she's working with. When she uses Print Preview for the worksheet, the page shown in the Print Preview dialog is always the first page. Michelle wonders if there is a way to make Print Preview automatically show the page of the worksheet on which she's working.
One way to approach this problem is to try working with print areas. If you select the area of the worksheet on which you are working and then set the print area equal to your selection, then when you display Print Preview, you should see only that print area.
If this does not accomplish your desire, then you might think you can resort to using a macro. Here's an example macro that may do the trick for you:
Sub PrintPreviewActivePage() Dim lActiveRow As Long Dim iActiveCol As Integer Dim iHPBs As Integer Dim iVPBs As Integer Dim lRow As Integer Dim iCol As Integer Dim iPage As Integer lActiveRow = ActiveCell.Row iActiveCol = ActiveCell.Column ActiveSheet.UsedRange If IsEmpty(ActiveCell.SpecialCells(xlCellTypeLastCell)) Then _ ActiveCell.SpecialCells(xlCellTypeLastCell).FormulaR1C1 = " " If lActiveRow > ActiveCell.SpecialCells(xlCellTypeLastCell).Row Or _ iActiveCol > ActiveCell.SpecialCells(xlCellTypeLastCell).Column Then _ Exit Sub With ActiveSheet iHPBs = .HPageBreaks.Count iVPBs = .VPageBreaks.Count lRow = 0 iCol = 0 If iHPBs > 0 Or iVPBs > 0 Then For lRow = iHPBs To 1 Step -1 If .HPageBreaks(lRow).Location.Row <= lActiveRow Then Exit For Next lRow For iCol = iVPBs To 1 Step -1 If .VPageBreaks(iCol).Location.Column <= iActiveCol Then Exit For Next iCol End If iPage = (lRow + 1) + (iCol * (iHPBs + 1)) .PrintOut From:=iPage, To:=iPage, Preview:=True MsgBox "Previewed page " & iPage End With If ActiveCell.SpecialCells(xlCellTypeLastCell).FormulaR1C1 = " " Then _ Selection.SpecialCells(xlCellTypeLastCell).ClearContents End Sub
This macro is actually a variation on any number of macros you could find with some sleuthing on the Internet. There are two key parts to it—first the macro figures out which "page" you are on in the worksheet, and then it uses the .PrintOut method with the Preview parameter set to True, resulting in Print Preview being invoked.
One some worksheets this macro may work great, but it is quite fickle in whether it will work or not. In most of my testing, I was not able to get it to work, unless I used very small worksheets. (In other words, very few rows and columns.) If you run it on a large worksheet, you'll quickly see that you get a "Subscript Out of Range" error in the loop that examines members of the .HPageBreaks collection. How this could happen when you aren't using a member that is out of range (lRow never varies outside the value returned by the .Count property) is baffling.
It seems to be a problem that Microsoft acknowledges, though. In fact, it is a problem they have acknowledged, yet never fixed, for years:
https://support.microsoft.com/en-us/help/210663/
The suggested solution on the web page doesn't really work, though. So, we are stuck with a macro that only works reliably on worksheets where you wouldn't need to calculate the page number because you are only working with a single page. Aargh!
The bottom line is that a macro-based approach may—for the foreseeable future—not be viable for Michelle's needs. That leaves us with just the print-area approach, described at the beginning of this tip.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13578) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
When printing a worksheet, you may want to rotate the output on the page to fit a certain orientation. Excel doesn't ...
Discover MoreAdding a watermark to the background of a printout can be challenging. This tip explains the different ways you can ...
Discover MoreGot a bunch of worksheets and you want to save paper by printing multiple worksheets on a single piece of paper? There ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2022-11-24 22:42:35
Tomek
I forgot to say that the print area set in p.1 should cover all what needs to be printed. The count of breaks will only apply to what is within the Print Area not the whole sheet. My guess is that this is where the Subscript-out-of-range error ultimately comes from.
If the print area does not include top and left part of your sheet, you may want to check whether your code selects the correct page to be printed/previewed.
As for the PrintCommunication setting, it makes sure that both printer and Excel agree on pagination. The statement enforces exchange (two-way) of information between Excel and the printer; sometimes it may get out of sync.
2022-11-24 22:12:45
Tomek
I remember seeing the Subscript-out-of-range error when I was trying to use HPageBreaks.Location or VPageBreaks.Location in the code. A that time I managed to avoid this error by including in the macro the following elements in the order specified below:
1. Explicitly setting the print area, and not depending on Excel to figure it out:
ActiveSheet.PageSetup.PrintArea = Range(<TopLeft>, <BottomRight>).Address 'necessary for proper finding of all page breaks
2. Adding the following line of code:
Application.PrintCommunication = True
3. Only after 1. and 2. placing the code to get the count of breaks:
With ActiveSheet
iHPBs = .HPageBreaks.Count
iVPBs = .VPageBreaks.Count
'[other code....]
End With
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2023 Sharon Parq Associates, Inc.
Comments