Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

HOW TO: Set a Print Area by Using a Defined Name in Excel 2000


View products that this article applies to.

Summary

In Microsoft Excel, if you have a macro that prints a range of cells on your worksheet, it may not print all of your data if you insert rows in that range. However, you can create a print macro that automatically adjusts when you insert rows. To do this, use a print area that refers to a defined name on your worksheet instead of using a specific range of cells to print.

This article includes a sample macro that illustrates how to set a print area by using a defined name.

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.

Set a Print Area by Using a Defined Name

To follow the example that is provided in this article, enter any text information into 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, the macro code reads as follows:
   Sub Macro1()
      Range("A1:D5").Select
      ActiveSheet.PageSetup.PrintArea = "$A$1:$D$5"
      ActiveWindow.SelectedSheets.PrintOut Copies:=1
   End Sub
				

The problem with this macro is that if you insert one or more rows in this range of cells and then rerun the macro, it prints only the cell range A1:D5. The macro does not print any rows that were moved down because of the rows that you inserted.

To have your macro automatically adjust when you insert or delete rows in the range you want to print, create a defined name for the range, and then use the defined name in your macro. For this example, follow these steps:
  1. In your worksheet, select the range A1:D5.
  2. On the Insert menu, point to Name, and then click Define.
  3. In the Names in workbook box, type myrange. Make sure that the Refers to box contains =Sheet1!$A$1:$D$5.
  4. Click OK.
  5. Modify the recorded macro that is shown at the beginning of this section so that it reads as follows:
          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 you now insert one or more rows in this range, the range of cells referred to by the defined name "myrange" expands accordingly. When you run the macro, all of the rows in this range are printed.

↑ Back to the top


References

For more information about defining a name for a range of cells, click Microsoft Excel Help on the Help menu, type Name cells in a workbook in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

↑ Back to the top


Keywords: KB213648, kbdtacode, kbprogramming, kbhowtomaster

↑ Back to the top

Article Info
Article ID : 213648
Revision : 4
Created on : 6/23/2005
Published on : 6/23/2005
Exists online : False
Views : 228