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 August 1, 2017)

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

Adjusting Column Width Using Menus

If you want to resize the width of your table columns, you can do it using a mouse, but you can get more precise widths by ...

Discover More

A Picture Is Worth a Thousand Words

Nothing beats a screen shot when you are trying to convey information about using the computer. With just a couple of easy ...

Discover More

Renaming a File

Need to rename a disk file from within a macro? You can do it using the Name command, described in this tip.

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Inserting the Current Time with Seconds

If you need to insert the current time, with seconds, then you'll need the macro discussed in this tip. It's easy to use and ...

Discover More

Deleting Worksheet Code in a Macro

When creating an application in VBA for others to use, you might want a way for your VBA code to modify or delete other ...

Discover More

Using Macros in Protected Workbooks

Having problems with using macros in a protected workbook? There could be any number of causes (and solutions) as described ...

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

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. Maximum image size is 6Mpixels. 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 four less than 4?

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.