Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Pulling Cell Names into VBA.

Pulling Cell Names into VBA

by Allen Wyatt
(last updated July 13, 2013)

5

If you have used Excel for any length of time, you undoubtedly know that you can define names in your worksheets that refer to various cells and ranges of cells. You can even define names that refer to constants and to formulas. (The naming abilities of Excel are really quite handy.)

As you are developing macros, you may wonder if there is a way to retrieve a list of defined names within a worksheet. This is actually quite easy, if you remember that the defined names are maintained in the Names collection, which belongs to the Workbook object. With this in mind, you can use the following code to put together a variable array that consists of all the names in a workbook:

    Dim NamesList()
    Dim NumNames As Integer
    Dim x As Integer

    NumNames = ActiveWorkbook.Names.Count

    ReDim NamesList(1 To NumNames)

    For x = 1 To NumNames
        NamesList(x) = ActiveWorkbook.Names(x).Name
    Next x

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (5676) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Pulling Cell Names into VBA.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Hyphenating Your Document

One of the final touches you can add to a document is to hyphenate it. This allows text to flow more smoothly from line to ...

Discover More

Removing Automatic Lines

Type a few dashes, underscores, or equal signs, and you could end up with a full-width line in your document. This is normal ...

Discover More

Jumping Around Folders

If you need to move between two different folders quite regularly in the Open dialog box, you'll find the technique described ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

MORE EXCELTIPS (RIBBON)

Switching Windows in a Macro

When you have multiple workbooks open at the same time, Excel allows you to easily switch between those workbooks. How you do ...

Discover More

Running a Macro when a Worksheet is Deactivated

When you change from one worksheet to another, you may want to have Excel automatically run a macro for the worksheet you are ...

Discover More

Getting Rid of Alphabetic Characters

When you need to get rid of characters in the middle of a cell value, the best way to do it is through the use of macros. ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments for this tip:

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 7 + 8?

2013-07-17 21:20:30

Bob Davey

Don,

Not sure where you are coming from here? The Macro returns every name in the workbook, together with its worksheet name and cell reference, also any appended comment. What other info would you like it to include?


2013-07-16 10:39:47

Don

OK...this is embarassing. Never mind all that gobbledygook about ValidWorkbookParameter and WorkbookParameter. I've now confused myself and wish I could take it all back, except the first and last paragraph of the first comment I made.


2013-07-16 10:29:23

Don

Correction: It is "WorkbookParameter" not "ValidWorkbookParameter"


2013-07-16 10:26:20

Don

One thing to add: Names can also be scoped to the worksheets.

Therefore, if you want just the Names scoped across the whole workbook (I think of them as the "global" Names), include a check for Names(x).ValidWorkbookParameter = TRUE

Another way to identify worksheet-scoped Names is to check for an exclamation point in Names(x).Name. to the left of the exclamation point is the name of the worksheet, similar to references to ranges on other worksheets.


2013-07-14 00:25:22

Bob Davey

The code below is a little more elaborate, expands the information available and produces a hard copy. Great for isolating names that a no longer functioning.

Sub ListAllNames()
t = "List all Names"
On Error GoTo EH
With ActiveWorkbook
If .Names.Count = 0 Then
MsgBox "There are no named ranges in this workbook.", 16, t
GoTo RE
End If
.Sheets.Add After:=Worksheets(Worksheets.Count)
ActiveSheet.Name = "WBNames" & Worksheets.Count
With ActiveSheet
.Range("A1").Value = " Range Name "
.Range("B1").Value = " Sheet & Cell Reference "
.Range("C1").Value = " Named Range Comment "
.Range("D1").Value = " Listing Created " & Now
Range("A1:D1").Select
Selection.Font.Bold = True
Range("A1").Select
Application.CutCopyMode = False
With .Range("A2")
For i = 1 To ActiveWorkbook.Names.Count
.Offset(i - 1, 0).Value = ActiveWorkbook.Names(i).NameLocal
.Offset(i - 1, 1).Value = ActiveWorkbook.Names(i)
.Offset(i - 1, 2).Value = ActiveWorkbook.Names(i).Comment
Next
With .CurrentRegion
.Columns.AutoFit
.Sort key1:=ActiveCell, Header:=xlYes
End With
End With
End With
End With
GoTo RE
EH:
MsgBox Error, 16, t
RE:
End Sub


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.

Links and Sharing