Microsoft KB Archive/213389

= How to programmatically copy all range names in Excel =

Article ID: 213389

Article Last Modified on 3/21/2007

-

APPLIES TO


 * Microsoft Office Excel 2007
 * Microsoft Office Excel 2003
 * Microsoft Excel 2002 Standard Edition
 * Microsoft Excel 2000 Standard Edition
 * Microsoft Excel 97 Standard Edition

-



This article was previously published under Q213389



SUMMARY
In Microsoft Office Excel, you can create a Microsoft Visual Basic for Applications (VBA) 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. 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. However, they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

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:  Start Excel, and then in a new workbook, click select cell A1. Perform one of the following actions:  In Microsoft Office Excel 2007, click the Formulas tab, and then click Define Name in the Defined Names group. In Microsoft Office Excel 2003 in and earlier versions of Microsoft Excel, point to Name on the Insert menu, and then click Define.  In the Define Names dialog box, in the Names In workbook box, type Range1, and then click OK. Select cell B1.</li> Perform one of the following actions:  In Excel 2007, click the Formulas tab, and then click Define Name in the Defined Names group.</li> In Excel 2003 and in earlier versions of Excel, point to Name on the Insert menu, and then click Define.</li></ul> </li> In the Define Names dialog box, in the Names In workbook box, type Range2, and then click OK.</li> Press ALT+F11 to start the Visual Basic editor.</li> On the Insert menu, click Module.</li>  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" or "Book2.xlsm"). ' "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 </li> Press ALT+F11 to return to Excel.</li> Perform one of the following actions:  In Excel 2007, click the Microsoft Office Button, point to Save As, click Excel Macro-Enabled Workbook, and then save the workbook as Book1.xlsm.</li> In Excel 2003 and in earlier versions of Excel, click Save on the File menu, and then save the workbook as Book1.xls.</li></ul> </li> Perform one of the following actions:  <li>In Excel 2007, click the Microsoft Office Button, click New, click Blank Workbook, and then click Create.</li> <li>In Excel 2003, click New on the File menu, and then click Blank workbook in the New Workbook task pane.</li> <li>In Excel 2002 and in earlier versions of Excel, click New on the File menu, click Workbook, and then click OK.</li></ul> </li> <li>Perform one of the following actions: <ul> <li>In Excel 2007, click the Microsoft Office Button, point to Save As, click Excel Macro-Enabled Workbook, and then save the workbook as Book2.xlsm.</li> <li>In Excel 2003 and in earlier versions of Excel, click Save As on the File menu, and then save the workbook as Book2.xls.</li></ul> </li> <li>Switch to Book1.

Note You may have to minimize or restore Book2 to see the Book1 button.</li> <li>Perform one of the following actions: <ul> <li>In Excel 2007, click the Developer tab, and then click Macros.

Note If the Developer tab is not displayed, click the Microsoft Office Button, click Excel Options, click Popular, click to select the Show Developer tab in the Ribbon check box, and then click OK.</li> <li>In Excel 2003 and in earlier versions of Excel, point to Macro on the Tools menu, and then click Macros.</li></ul> </li> <li>In the Macro name list, click Copy_All_Defined_Names, and then click Run.</li> <li>Switch to Book2 and note that cell A1 is named Range1, and that cell B1 is named Range2.</li></ol>

Additional query words: XL97 XL2000 XL2002 XL2003 XL2007

Keywords: kbmacro kbautomation kbvba kbexpertisebeginner kbdtacode kbhowto kbprogramming kbualink97 KB213389

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.