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)

8

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

Setting Limit Height in the Equation Editor

You can adjust the distance between the equation body and a limit line.

Discover More

Rounding to Two Significant Digits

Excel provides a variety of functions you can use to round values in any number of ways. It does not, however, provide a way ...

Discover More

Undesired Font in Form Fields

If you get unwanted formatting in your form fields, it could be because of the way you are formatting the line on which the ...

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)

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

Discover More

Telling which Worksheets are Selected

If your macro processes information on a number of worksheets, chances are good that you need your macro to figure out which ...

Discover More

Exiting a For ... Next Loop Early

If you use For ... Next loops in your macros, make sure you give a way to jump out of the loop early. That way you can limit ...

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 less than 7?

2017-11-23 05:50:59

Barry

@Faizan,

Sorry the offset should be -9 for the last nine cells,
If you want the nine cells below the last entry in column A use:

With Worksheets("NCD Margins - Comp").Range("A" & Rows.Count).End(xlUp)
.Range(.Offset(1), Offsett(9)).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End With


2017-11-23 05:46:24

Barry

@Faizan

Your code only specifies a single cell, "Worksheets("NCD Margins - Comp").Range("A" & Rows.Count).End(xlUp).Offset(9)." is not a range.
Assuming you want to paste into the last 9 cells of column A the following code should work for you:

With Worksheets("NCD Margins - Comp").Range("A" & Rows.Count)
.Range(.End(xlUp), .End(xlUp).Offset(9)).PasteSpecial _
Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End With

It also has a potential problem if the "Worksheets("NCD Margins - Comp").Range("A" & Rows.Count).End(xlUp)." ends in row 9 or less.


2017-11-23 00:12:05

Faizan

Hello,

I am trying to paste the copied value to multiple cells (in a list style) from 1 to 9 but the following code only paste the value into one cell.

Please can you help me fix this code to achieve what I like it do..

Thanks

Worksheets("NCD-Copy").Range("D31").Copy
Worksheets("NCD Margins - Comp").Range("A" & Rows.Count).End(xlUp).Offset(9).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False


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.