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 January 6, 2018)

1

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.

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

Hiding Table Gridlines, by Default

The edges to table cells are shown two ways in Word: gridlines and borders. Table gridlines are only seen in Word; they ...

Discover More

Moving Part of a Footer Down a Line

Setting up a single footer line for your printouts is fairly easy. If you want to move part of the footer down a line so ...

Discover More

Endnotes in a Separate Document

When you add endnotes to a document they are normally positioned (as one would expect) at the end of the document. You ...

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)

Error Using ATAN2 Function in Macro

Excel allows you to use worksheet functions from within macros. This is helpful, especially when you are trying to ...

Discover More

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

Copying Worksheets in a Macro

Copying worksheets (one or many) is easy to do manually. What is not well known is that it is even easy to make the ...

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 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 seven minus 2?

2018-02-25 19:45:35

Mike

I have a VBA code written to expand multiple groups of rows depending on if one of three selections is selected yes. I currently have the worksheet setup with 10 blank rows to expand if the yes option is selected, but want the ability to add rows if more data rows are needed beyond ten. The current code only references Rows to hide(i.e. 9:18) so if i insert another row, the code wont expand or collapse it and would mess up the ranges of the rows lower in the worksheet.

It seems like referencing a named range would work as the code wouldn't be looking for a range of row numbers, but rather a defined named range which would allow for rows to be inserted and dynamically update. I tried various inputs of the named range wording but keep getting an error in the coding. Can you let me know what the proper coding would be to reference the named range "SurveyMapping"? Code Section with attempted named range and a section with row number references Below:

If Not Intersect(Target, Range("$A1:ZZ100")) Is Nothing Then

If Range("b51").Value = "Yes" Then
Range("[Market Analysis Tool-2018-02-25.xlsm]!SurveyMapping").EntireRow.Hidden = False
ElseIf Range("c51").Value = "Yes" Then
Range("[Market Analysis Tool-2018-02-25.xlsm]!SurveyMapping").EntireRow.Hidden = False
ElseIf Range("d51").Value = "Yes" Then
Range("[Market Analysis Tool-2018-02-25.xlsm]!SurveyMapping").EntireRow.Hidden = False
Else
Range("[Market Analysis Tool-2018-02-25.xlsm!]SurveyMapping").EntireRow.Hidden = True
End If

End If

If Not Intersect(Target, Range("$A1:ZZ100")) Is Nothing Then

If Range("b64").Value = "Yes" Then
Rows("65:74").EntireRow.Hidden = False
ElseIf Range("c64").Value = "Yes" Then
Rows("65:74").EntireRow.Hidden = False
ElseIf Range("d64").Value = "Yes" Then
Rows("65:74").EntireRow.Hidden = False
Else
Rows("65:74").EntireRow.Hidden = True
End If

End If


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.