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)

5

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

Inserting the User's Address

If you enter your address into Word, you can insert that address anywhere you want in a document by using a single field. ...

Discover More

Weird Actions for Arrow Keys and Enter

If your arrow keys and the Enter key aren't working as you expect them to, the problem could have any number of causes. This ...

Discover More

No-border Text Boxes by Default

Text boxes can be a great design element when laying out your documents. If you want those text boxes to have no borders (or ...

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)

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

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

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 you ...

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

2017-08-14 04:08:30

Barry

@Jane

This worked OK for me provided the range "Test" was not within 3 rows of the top of the worksheet. This is because an offset of -3 would be beyond the bounds of the worksheet and so produces an error.

This error will occur at any of the boundaries of the worksheet but more commonly with offsets that resolve to a column left of Column A, and as in the case above to a row above Row 1. Most spreadsheet Users rarely use cells near the bottom edge or right-hand edge of a worksheet.


2017-08-13 05:27:43

Jane

What if you wanted to select a range of cells relative to a named range "Test"

Range(Range("Test").Offset(-3, 5), Range("Test").Offset(0, 10)).Select
do not seems to work


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.