Written by Allen Wyatt (last updated December 5, 2020)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
Grahame has a cell (C7) that contains a range, such as B8:B207. Whenever the range changes in C7, he needs a way to change the print range to match what is in that cell. However, Grahame is unsure of how to set the print range within the macro code.
Setting a print range within a macro is quite easy; you can do it in this manner:
ActiveSheet.PageSetup.PrintArea = "$A$1:$D$23"
Note that all you need to do is to assign to the PrintArea property a range that you want used for your print range. Thus, if cell C7 contains a range (like B8:B207), you could do it in this manner:
ActiveSheet.PageSetup.PrintArea = Range("C7").Value
You can, if you desire, create an event handler that will change the print area every time that cell C7 is changed. To do so, just add this to the VBA module for the worksheet. (Just right-click the sheet tab and choose View Code, then add the macro there.)
Private Sub Worksheet_Change(ByVal Target As Range) On Error Resume Next If Target.Address = "$C$7" Then ActiveSheet.PageSetup.PrintArea = Target.Value End If End Sub
The Worksheet_Change event handler is automatically run every time there is a change in the worksheet. If the cell being changed is C7, then the PrintArea property is updated to reflected whatever is in cell C7.
Even though Grahame asked for how to set the print range in a macro, it should be noted that you can automatically set the print range without using a macro.
Now, anytime you print, the print range will be grabbed from whatever is in cell C7. Note that you should change the formula, in step 5, to use whatever sheet name you specified in step 4. (In place of the Sheet1 sheet used in the example.)
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13807) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
Need to print more than one portion of your worksheet? If you use named ranges for the different ranges you want to ...
Discover MoreExcel allows you to specify an area of your worksheet that should be printed. Here's how to "lock" that area so it cannot ...
Discover MoreNeed to print several portions of a worksheet all on a single piece of paper? Here's an easy way you can get what you ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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 © 2024 Sharon Parq Associates, Inc.
Comments