XL: How to Set Print Area to Range with Defined Name

Last reviewed: February 3, 1998
Article ID: Q141095
The information in this article applies to:
  • Microsoft Excel 98 Macintosh Edition
  • Microsoft Excel 97 for Windows
  • Microsoft Excel for Windows 95, versions 7.0, 7.0a
  • Microsoft Excel For Windows, versions 5.0, 5.0c
  • Microsoft Excel for the Macintosh, versions 5.0, 5.0a

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 have inserted rows within the range specified in your macro. However, if rather than hard- coding a specific range of cells to print, you instead use a print area that refers to a defined name on your worksheet, you can create a print macro that automatically adjusts to any additional rows you insert into the range.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and 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 needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/default.asp

To follow the example 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, you create a macro similar to the following:

   Sub Macro1()
      Range("A1:D5").Select
      ActiveSheet.PageSetup.PrintArea = Selection.Address
      ActiveWindow.SelectedSheets.PrintOut Copies:=1
   End Sub

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

In order to have your macro automatically adjust when you insert or delete rows within 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, you would do the following:

  1. In your worksheet, select the range A1:D5.

  2. On the Insert menu, point to Name, and then click Define.

  3. In the Define Name dialog box, type "myrange" (without the quotation marks) 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" (without the quotation marks).

  4. Click OK.

  5. Modify the above recorded macro so it reads:

          Sub Macro1()
             Range("myrange").Select
             ActiveSheet.PageSetup.PrintArea = Selection.Address
             ActiveWindow.SelectedSheets.PrintOut Copies:=1
          End Sub
    
       NOTE: Change only the first line of code. Instead of using a fixed range
       of cells, use the defined name "myrange".
    
    
If you now insert one or more rows within this range, the range of cells referred to by the defined name "myrange" will grow accordingly, and if you run your macro, all of the rows in this range will be printed.

REFERENCES

For more information about Defined Names, click Answer Wizard on the Help menu, and type:

   tell me how to define a name


Additional query words: 5.00 5.00a 5.00c 7.00 XL98 XL97 XL7 XL5
Keywords : kbcode kbprg PgmHowto
Version : WINDOWS:5.0,5.0c,7.0,97; MACINTOSH:5.0,5.0a,98
Platform : MACINTOSH WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 3, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.