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

Combining Word Documents

At some point you may want to insert one Word document inside another Word document. An easy way to do this is to use the ...

Discover More

Sudden Increases in Workbook File Size

Workbooks can get rather large rather quickly. If you think your workbook has gotten too big too fast, here are some things ...

Discover More

Creating an Organization Chart

Graphics are often added to worksheets to make it easier to understand the data contained in the worksheet. Sometimes, ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Counting Commas in a Selection

If you have a range of cells in which you want to count all the commas, there are several ways you can derive the figure you ...

Discover More

Mouse Click Event in VBA

Need to know if a particular cell is clicked with the mouse? Excel has no particular event handler for clicking in this way, ...

Discover More

Copying Named Ranges

Named ranges are a great tool to use in developing formula-heavy workbooks. You may want, at some point, to copy your named ...

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. 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 9 + 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.