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

Written by Allen Wyatt (last updated September 16, 2021)
This tip applies to Excel 97, 2000, 2002, and 2003


13

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.

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

Changing Font Sizes

Want to change the size of the font within a worksheet? Excel allows you to choose from a list of sizes, as well as ...

Discover More

Highlighting Text Using the Keyboard Only

Highlighting text, using the Highlight tool, is a great way to mark up a document. Normally you need to use the toolbar ...

Discover More

Inserting Special Spaces

Do you need to frequently add en spaces and em spaces to your documents? You can add special tools to Word that make ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (menu)

Understanding the For ... Next Structure

Part of the power of VBA is being able to control when some of your code executes and when it doesn't. A primary way to ...

Discover More

Understanding Macros

What is a macro? Ever wonder what these are and how to use them? This tip answers the basics of what a macro is used for, ...

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

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}] (all 7 characters, in the sequence shown) 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 three minus 2?

2022-09-14 09:40:11

ADIL

Thank you Sir


2020-10-19 13:29:12

Jeronimas

Thank you Sir


2019-08-09 10:56:39

Vladimir

Thanks a lot !!!!!!!!!


2019-08-01 03:06:18

Barry

@Nicholas

In Excell VBA cell references are enter a text values so do not adjust if rows or columns are added tk the worksheet. To get around this name the cell, E10, and use the name instead od the cell reference in the macro. If you Name cell E10 as "TargetCell' then macro becomes:
Set Target = Range("TargetCell")


2019-07-31 16:25:44

Nicholas

Hello,

My question is I have set up a Macro that has a very specific Target Range (see below). A large portion of my workbook such as different tabs is based on the value in this specific cell. This is great until someone adds a column or row and then the whole workbook doesn't work b/c the macro is pulling from that "Target range," but the value I want to base the rest of the workbook on is now 1 cell moved. Is there a way to make the Target.Range change with the workbook. Kinda the same way an equation changes to keep the right cells if a row or column is added.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Set Target = Range("E10")
If Target.Value = "1" Then
Call One
End If
If Target.Value = "2" Then
Call Unhide
Call Two
End If


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.