Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. 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

Written by Allen Wyatt (last updated August 10, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


3

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

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (5676) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. 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

Entering Characters with Diacritical Marks

Entering characters that use diacritical marks is easy as pie in Word and some other programs. Not so in Excel, it takes ...

Discover More

Default Worksheet when Opening

When opening a workbook, you may want to make sure that a particular worksheet is always displayed first. The only way to ...

Discover More

Capitalizing the First Letter after a Colon

There are many rules in English grammar (and many exceptions to those rules.) One common rule of grammar is to capitalize ...

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)

Converting Phone Numbers

Sometimes you receive a phone number that contains alphabetic characters and you need to convert it to a purely numeric ...

Discover More

Specifying Location for a Message Box

When writing macros, you may want to position a message box at a specific location on the screen. This can't be done in ...

Discover More

Automatically Enabling Macros for Specific Workbooks

On your system you may have workbooks that contain macros you know are safe to use. Microsoft provides two things you can ...

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}] (all 7 characters, in the sequence shown) 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 6 + 7?

2021-04-23 10:33:43

J. Woolley

You might be interested in this freely available array function in My Excel Toolbox:
ListNames([Scope], [SkipHidden], [SkipHeader])
ListNames is most useful as a dynamic array in newer versions of Excel. You can also use it like this in older versions of Excel that do not support dynamic arrays:
SpillArray(ListNames([Scope], [SkipHidden], [SkipHeader]))
SpillArray will determine and populate the spill range for its array expression argument, simulating a dynamic array.
See https://sites.google.com/view/MyExcelToolbox/


2020-06-18 10:29:16

J. Woolley

@Philip
ReDim must be used at the procedure level (Sub, Function, or Property). Try a Collection instead. See https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/collection-object
or https://wellsr.com/vba/2018/excel/the-vba-collection-object/

For example:

Dim C As Collection ' Public at module level
Sub testGlobalA()
Set C = New Collection
For n = 0 To 9
C.Add (n * 3)
Next n
testGlobalB
End Sub
Sub testGlobalB()
For Each n In C
Debug.Print n
Next n
End Sub


2020-06-17 02:55:54

Philip

Can I "redim" a Public array variable somehow ? I need to pull data into public arrays, but the length of the arrays will vary depending on when the code runs ... when declaring a Public array variable (e.g. Public arrFieldNames() as String), and then in a different sub I am re-dimming them, they seem to lose there "Public" status and go "out of context" once the sub ends ...


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.