Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier 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 October 3, 2015)

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.

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 (11402) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel 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

Quickly Dumping Array Contents

Variable arrays are used quite often in macros. If you use an array once in your macro and then need to reuse it for ...

Discover More

Spacing Table Rows Vertically

Want to get just the right amount of spacing above and below text in a table cell? A very easy way to do this is to ...

Discover More

Making AutoCorrect Automatically Recognize the Replace Word

When you select some text in your document and then display the AutoCorrect dialog box, it can seem a bit odd that ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Stepping Through a Non-Contiguous Range of Cells

Using macros to step through each cell in a selection is a common occurrence. What if that selected range is made up of ...

Discover More

Controlling Window Size when Opening Additional Workbooks

When you open multiple workbooks, the way in which Excel sizes them is not the best for your needs. This tip looks at a ...

Discover More

Automating Copying Macros

You can manually copy macros from one workbook to another, but what if you want to automate the copying process? Here's ...

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

2016-12-15 11:11:50

eric

In the meantime I succeeded to put together a solution. I suppose it's very clumsy, but it works perfectly :)

Range(ActiveCell, Cells.Find(What:="abc", After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)).Select
Selection.Delete Shift:=xlUp
ActiveCell.Offset(1, 0).Range("A1").Select


2016-12-12 16:37:55

eric

Excel 2016. I have to select a range relatively to the current cell. From the current cell up to the first cell containing the string "abc" (by columns). Then delete selection and moving cells up.

I am not so skilled with VBA, so I will appreciate a lot some guidance.

Thank you very much


2016-09-14 07:52:14

Willy Vanhaelen

@ jens jensen

Range(Cells(1, 1), Cells(32, 8)).Select
or
Range(Range("A1"), Range("H32")).Select

Both will do the job.


2016-09-13 19:58:28

jens jensen

I was making a print button for a sheet and it should always select 8 columns and 32 rows from the top left cell and print only that area.

I can't make it work
I'm sure it's easy but then I see baltthamossa2b saying that if my macro needt to select cells I'm doing something wrong.
...


2015-10-05 04:47:00

balthamossa2b

As a general rule, if your macro needs to select cells it means you are doing something wrong. Or you took a macro straight from the macro recorder and didn't optimize it.

That being said, Range.Offset is my preferred way of navigating around.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu 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.