XL: How to Programmatically Copy All Range Names (213389)



The information in this article applies to:

  • Microsoft Excel 2002
  • Microsoft Excel 2000
  • Microsoft Excel 97 for Windows

This article was previously published under Q213389

SUMMARY

In Microsoft Excel, you can create a Microsoft Visual Basic for Applications Sub procedure (macro) that you can use to copy all the range names from the active workbook to another workbook.

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 professionals 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 a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site: For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site: To create and use a macro to copy all of the range names from the active workbook to another workbook, use the steps in the following example:
  1. Start Excel, and then in a new workbook, click select cell A1.
  2. On the Insert menu, point to Name, and then click Define.
  3. In the Define Names dialog box, in the Names In workbook box, type Range1, and then click OK.
  4. Select cell B1.
  5. On the Insert menu, point to Name, and then click Define.
  6. In the Define Names dialog box, in the Names In workbook box, type Range2, and then click OK.
  7. Press ALT+F11 to start the Visual Basic editor.
  8. On the Insert menu, click Module.
  9. On the module sheet, type the following code:
    Sub Copy_All_Defined_Names()
       ' Loop through all of the defined names in the active
       ' workbook.
         For Each x In ActiveWorkbook.Names
          ' Add each defined name from the active workbook to
          ' the target workbook ("Book2.xls").
          ' "x.value" refers to the cell references the
          ' defined name points to.
          Workbooks("Book2.xls").Names.Add Name:=x.Name, _
             RefersTo:=x.Value
       Next x
    End Sub
    					
  10. Press ALT+F11 to return to Excel.
  11. On the File menu, click Save.
  12. In the File name box, type Book1.xls, and then click Save.
  13. On the File menu, click New.
  14. Click Workbook, and then click OK.
  15. On the File menu, click Save.
  16. In the File name box, type Book2.xls, and then click Save.
  17. Switch to Book1.

    NOTE: You may have to minimize or restore Book2 to see the Book1 button.
  18. On the Tools menu, point to Macro, and then click Macros.
  19. In the Macro name list, click Copy_All_Defined_Names, and then click Run.
  20. Switch to Book2 and note that cell A1 is named Range1, and that cell B1 is named Range2.

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbdtacode kbhowto kbProgramming kbualink97 KB213389