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

Tying a Hyperlink to a Specific Cell

Make a hyperlink to a cell in your workbook, edit the structure of that workbook a bit, and you may find that the ...

Discover More

Setting Grid Line Intervals for a Radar Chart

Excel provides a wide varity of chart types you can use with your data. Unfortunately, this variety can often make it ...

Discover More

Determining an Integer Value

One of the math functions you can use in your macros is the Int function. It provides a way for you to derive an integer ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling 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

Automatically Changing References to VBA Libraries

VBA libraries are used to provide some functions to your macros. These libraries are often tied to the version of Excel ...

Discover More

Hiding Entries in an InputBox

Requiring users to input a password in Excel increases the security of the worksheet and can prevent someone from running ...

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 2 + 0?

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.