This article was previously published under Q141095
This article has been archived. It is offered "as is" and will no longer be updated.
In Microsoft Excel, if you have a macro that prints a range of cells onyour worksheet, it may not print all of your data if you have inserted rowswithin the range specified in your macro. However, if rather than hard-coding a specific range of cells to print, you instead use a print areathat refers to a defined name on your worksheet, you can create a printmacro that automatically adjusts to any additional rows you insert into therange.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. To follow the example provided in this article, enter any text informationinto the cell range A1:D5 on Sheet1 of a new workbook.
If you record a macro that selects the range of cells, sets the print area,and then prints the worksheet, you create a macro similar to one of the following:
Excel 5 for Windows, Excel 7 for Windows 95, or Excel 5 for the Macintosh
Sub Macro1() Range("A1:D5").Select ActiveSheet.PageSetup.PrintArea = Selection.Address ActiveWindow.SelectedSheets.PrintOut Copies:=1 End Sub
Excel 97 for Windows, Excel 98 Macintosh Edition
Sub Macro1() Range("A1:D5").Select ActiveSheet.PageSetup.PrintArea = "$A$1:$D$5" ActiveWindow.SelectedSheets.PrintOut Copies:=1 End Sub
NOTE: Macros recorded in these versions of Microsoft Excel set the Print Area to a range address rather than the more general Selection.Range.
The problem with this macro is that if you insert one or more rows withinthis range of cells, and then rerun the recorded macro, it still printsonly the range specified in the macro. The macro does not print any rowsthat were moved down because of the row or rows that you inserted.
In order to have your macro automatically adjust when you insert or deleterows within the range you want to print, create a defined name for therange, and then use the defined name in your macro. For this example, youwould do the following:
In your worksheet, select the range A1:D5.
On the Insert menu, point to Name, and then click Define.
In the Define Name dialog box, type myrange in the Names In Worksheet box at the top of the dialog box. Make sure that the Refers To box at the bottom of this dialog box contains =Sheet1!$A$1:$D$5.
Modify the above recorded macro so it reads:
Sub Macro1() Range("myrange").Select ActiveSheet.PageSetup.PrintArea = "myrange" ActiveWindow.SelectedSheets.PrintOut Copies:=1 End Sub
NOTE: Instead of using a fixed range of cells, use the defined name "myrange". If the second line reads:
ActiveSheet.PageSetup.PrintArea = Selection.Address as in versions prior to Excel 97, this does not have to be changed.
If you now insert one or more rows within this range, the range of cellsreferred to by the defined name "myrange" will grow accordingly, and if yourun your macro, all of the rows in this range will be printed.
For more information about Defined Names, click the Index tabin Microsoft Excel Help, type the following text
and then double-click the selected text to go to the "Name cells in aworkbook" topic.