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

Specifying Your Target Monitor

When you create a worksheet that is destined for viewing on the Web, you will want to specify the monitor resolution you ...

Discover More

Selecting a Text Block

Word has an interesting way of allowing you to select a rectangular block of text, without reference to what may be within ...

Discover More

Word Marks Mixed Case Acronyms as Incorrect

Many acronyms (such as DoD) are considered correct when they used mixed uppercase and lowercase. Word may not seem them as ...

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)

Swapping Two Strings

Strings are used quite frequently in macros. You may want to swap the contents of two string variables, and you can do so by ...

Discover More

Adjusting a Path Based on System and User

It is not uncommon to set variables in a macro based on other values, such as time or date. You could also set variables ...

Discover More

Copying Worksheet Code Automatically

When creating a workbook to be used by others, you may want any worksheets they add to the workbook to contain some special ...

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 8Mpixels. 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 - 0?

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.