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

Using Strikethrough Formatting

Need a line through the middle of your text? Use strikethrough formatting, which is easy to apply using the Format Cells ...

Discover More

Controlling Repagination in Macros

Want to turn off document repagination while your macro does its work? Here are two approaches you can use.

Discover More

Unwanted Page Breaks in Cross-References

You can use fields to cross-reference different text in a document. If you get unwanted results from the cross-referencing ...

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 (menu)

Writing a Macro from Scratch

Recording macros is a great approach to getting started with macros, but at some point you'll need to create one from ...

Discover More

Opening a Workbook but Disabling Macros

Macros that run automatically when you open or close a workbook are quite helpful. You may not want them to run, however, ...

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
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 6 - 3?

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.