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: Selecting a Range of Cells Relative to the Current Cell.

Selecting a Range of Cells Relative to the Current Cell

by Allen Wyatt
(last updated March 11, 2017)

3

Sometimes in a macro it is helpful to select cells relative to whichever cell is currently selected. For instance, let's say you want to select the first three cells of the current row. You can do that by using the following VBA code:

Range(Cells(Selection.Row, 1), Cells(Selection.Row, 3)).Select

The Cells property returns an object that represents a specific row and column (individual cell) of a worksheet. In this usage, Cells is used twice to determine a specific range of cells. The first instance returns the first cell of the current row, while the second returns the third cell of the current row. Thus, the range becomes the first through third cells of the current row.

Instead of using the Cells property to specify a location, you can use the Offset property to accomplish much of the same task. Consider the following code:

Range(ActiveCell.Offset(-3, 5), ActiveCell.Offset(0, 10)).Select

This uses the Offset property of the ActiveCell object to specify a range relative to the currently selected cell. The Offset property takes an argument that represents the row and column of the offset. A negative value represents up (for the row) and left (for the column). A positive value is down (for the row) and right (for the column). You can also use a value of 0, which represents the current row or column.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2268) 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: Selecting a Range of Cells Relative to the Current Cell.

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

Understanding Relative and Absolute Addressing

In Excel you can reference a cell in a formula by entering the coordinates for the cell you want to reference. This can ...

Discover More

Saving Common Formulas

Got some formulas you slaved over and want to use in lots of workbooks? This tip presents some helpful ideas on how you can ...

Discover More

Understanding AutoComplete

Entering data in a worksheet can be time consuming. One of the tools that Excel provides to make entry easier is ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

MORE EXCELTIPS (MENU)

Maximum Length Limit for a Macro

Make your macros too long, and Excel may just refuse to run them at all. This tip explains what the limit is for macros, and ...

Discover More

Selecting Visible Cells in a Macro

Many times you need to select just the visible cells before taking some action. It is helpful to know how to make this ...

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
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 for this tip:

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 9 - 7?

2017-04-25 09:08:05

Alan

Hi Barry,
I find Selection, ActiveCell, ActiveSheet and the such very useful in the first instance to refer to the thing currently “active”. ( For example to determine what someone may have selected as “where” something should go).
But I totally agree that in many cases it is a good idea to assign that “active” thing to a variable as soon as possible, after which refer to that rather than further using a reference to the “active” thing
I always prefer to do as you suggest.

Another reason for doing this is to be using specifically a Range object, or a property thereof.
I had a lot of discussions recently about whether or not we actually have a Cells(row, column) Property at all.
Cells returns a Range object of all the cells of the object to which it applies. Then we typically are using the Range object Property on that returned Range object.
So Cells(row, column) relies on implicit defaults etc to actually use the Range object Item Property on the returned Range object from the Cells Property
I got into some heated discussions with many experts who had split opinions about that.
So getting at an early stage to a Range object helped me circumvent all that .. lol.. – Everyone agreed then that I was working after that on a Range object. :)
Alan


2017-04-24 05:21:15

Barry

I am not in favour of using the Selection object as it is poor programming practice, and especially not moving it as it can confuse the User, and it can cause run-time errors. There are a few exceptions to this naturally.

Note: using the Selection object implies the top left cell for functions/methods that only require a single cell; more often than not this is also the Activecell but is is not necessarily the case. [Try selecting a range of cells on a worksheet then press the Tab key to see the Activecell move but the Selection remains the same.]

If you actually want to reference a range relative to the currently selected cell(s) then I would use the following:

Set rng = Selection.Offset(3,4) 'Note this preserves the range of cells if multiple cells are selected.

Use "Set rng = Selection.Range("A1").Offset(3,4) ' if you only want to reference a single cell relative the to the top left cell of the selection.

then in subsequent code use the 'rng' object instead of the 'Selection' object.

NOTE: if Option Explicit is used (highly recommended) then 'rng' will need to be defined using a DIM statement.


2017-04-23 05:11:04

Vempati Satya Suryanarayana

Hi Allen you are to the point and your explanation is simple. After a lot of search I found your tip. And my problem is solved


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.

Links and Sharing