Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Using Named Ranges in a Macro.

Using Named Ranges in a Macro

by Allen Wyatt
(last updated April 20, 2013)

8

Bruce has a named range (Account) defined in a workbook and he wonders how to access and use that named range from within a macro. There are several ways you can access the range, using either the Range object or the Names collection.

To access the named range using the Range object, all you need to do is provide the name of the range as a parameter to the object. This name is the same one that you defined within Excel. For instance, the following line could be used to change the interior color of the entire range:

Worksheets("Sheet1").Range("Account").Interior.Color = vbYellow

Note that the Range object is used relative to a particular worksheet, in this case Sheet1. You could also define a range object within VBA and then assign it to be equal to the named range, in this manner:

Set rng = Worksheets("Sheet1").Range("Account")

The other method of using the named range is to use the Names collection. The following line will again set the interior color of the range to yellow:

Workbooks("Book1.xls").Names("Account").RefersToRange.Interior.Color = vbYellow

Note that the Names collection is relative to the entire workbook, so it is not necessary to know which worksheet the named range is associated with when you use this method of access. You can also define a range object in VBA and assign it to be the same as the named range:

Set rng = Workbooks("Book1.xls").Names("Account").RefersToRange

You should know that the Names collection method of accessing a named range will only be viable if you don't have the same named range defined on different worksheets in the workbook. If you do, then you will need to use the Range object method, which requires the use of a specific worksheet name in the reference.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3106) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Using Named Ranges in a Macro.

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

Selecting the Entire Document with the Mouse

Want a quick way to select your entire document without taking your hand off of the mouse? Try clicking away using the ...

Discover More

Transposing Two Paragraphs

Need to swap two adjacent paragraphs? Your editing arsenal can include a command to do this is you use the macro in this tip.

Discover More

Deleting Commented Text

Word allows you to add comments to your document to aid in the development of the document. At some point you may want to ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (menu)

Understanding the For ... Next Structure

Part of the power of VBA is being able to control when some of your code executes and when it doesn't. A primary way to do ...

Discover More

Self-Deleting Macros

Macros are very powerful, but you may not want them to always be available to a user. Here are some ways you can limit their ...

Discover More

Offering Options in a Macro

It is often helpful to get user input within a macro. Here's a quick way to present some options and get the user's response.

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 6 + 4?

2016-12-28 21:06:43

Randy Downs

Nice reference. I ended up using ThisWorkbook since my macro ran within the active workbook.


2016-07-21 05:19:22

Yann

Hi,

I am trying to use a name range within the consolidate function but I get an error 1004.

Sub consolidate()
Dim conso As Range

Range("A3").Select
Selection.CurrentRegion.Select
Selection.Offset(2, 2).Select
Set conso = Selection

LastRow = Cells(Cells.Rows.Count, "C").End(xlUp).Row + 5
Range("C" & LastRow).Select


Selection.consolidate Sources:= _
Range("conso"), Function:= _
xlSum, TopRow:=False, LeftColumn:=True, CreateLinks:=False

End Sub '

I have tried to search online but couldn't find any info on the consolidate function and naming a range in the function.
Any thoughts?


2016-04-15 10:19:40

Florian

I am trying to retrieve the column number of a named range in a sheet, using a concatenation of two string object variable Raw_point and Raw_list_name, like this:

Run_value_column = Run_wb.Names(Raw_point & "_" & Raw_list_name).RefersToRange.column

Unfortunately the code does not work(Application-defined or object-defined error), although it does when I refer to the named range directly between "". Is there a way to use Names() with object variables?
Thank you.

Florian


2016-03-26 10:50:06

S One

Just to confrim, I use excel 2003 and have found this site very useful.


2015-09-02 05:39:35

Reza

When I set the range from another sheet that isn't active vba gives an error and macro doesn't proceed until I select that sheet.
How can I do that? Is it Possible


2015-03-16 15:23:50

awyatt

Scott,

But people still use the old versions. That's why this site exists.

If you want tips for the newer versions (including the ones you noted), click the links on this page that lead to those tips--there are plenty of them here!

-Allen


2015-03-16 15:13:43

Scott Lothrop

It's time to update your tips, current versions of Excel are 2010 and 2013. None of the versions you list are even supported.


2014-12-02 15:31:01

Bob Thing

The active workbook is ActiveWorkbook, so ActiveWorkbook.Names("Account") will access the named range "Account" in the current workbook.


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.