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

Automatically Determining a Due Date

When you are doing a mail merge in Word, you may need to calculate a date sometime in the future. Word doesn't include an ...

Discover More

Adding Captions

Need to add a caption after a document element such as a figure or table? You can add and format one manually, or you can ...

Discover More

Embedding Fonts in a Document

Fonts are essential to getting your text to look just the way you want it to look. If you have a font that you use in a ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

MORE EXCELTIPS (RIBBON)

Renaming a Macro

Got a macro that doesn't have quite the right name? You can rename the macro by following these simple steps.

Discover More

Setting Program Window Size in a Macro

The macro programming language used in Excel gives you a great many tools that allow you to modify the way that Excel appears ...

Discover More

Determining the Length of a String

Macros are great for working with strings, and one of the most commonly used string functions is Len. This tip explains how ...

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


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
Share