Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. 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: Relative VBA Selections.

Relative VBA Selections

by Allen Wyatt
(last updated July 2, 2018)

5

It is a common thing to need to select cells in a macro. What if you want to select a range of cells relative to your current location, however? It so happens that there are several ways you can accomplish this task. For instance, if you want to select a single cell, relative to your current location, you can use the Offset method. As an example, if you want to select the cell that is two rows down and one column to the right of your current location, you could use the following:

ActiveCell.Offset(2, 1).Select

If you want to select a larger range than just a single cell, you can combine the Offset method with the Address Method to find actual cell addresses, and then use your findings to actually select the range itself. For instance, you might want to select the range that begins two rows down and one column to the right, but then extends for four rows and three columns. You can accomplish this in the following manner:

StartCell = ActiveCell.Offset(2, 1).Address
EndCell = ActiveCell.Offset(5, 3).Address
Range(StartCell, EndCell).Select

An alternative method of accomplishing the same task is to use the Resize method. In this technique, you would first select the upper-right cell of the desired range (as was done in the first use of Offset, above), and then use Resize to change the size of the selection. This is how it is done:

ActiveCell.Offset(2, 1).Select
Selection.Resize(4, 3).Select

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

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

Displaying the Document Title Right Away

One of the properties that Word maintains for a document is a title. If you want this title displayed on the title bar ...

Discover More

Unable to Edit Document with Embedded Fonts

What are you to do if you embed fonts in a document and then someone else cannot make changes to that document? Chances ...

Discover More

Adding Borders to Cell Contents

Word allows you to quickly add borders to cells in a table, but you may not know that you can also add borders to the ...

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)

Generating a List of Macros

Got a workbook that has lots and lots of macros associated with it? Here's a way you can get a list of all of those ...

Discover More

Pulling Apart Characters in a Long String

You can easily use formulas to pull apart text stored in a cell. For instance, if you need to pull individual characters ...

Discover More

Macro Fails after Filter

When developing a macro that others may use, you might want to test it out to make sure it works properly if a filter is ...

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 six more than 7?

2018-03-01 06:21:12

Roy Taylor

If you want a value from a cell, but don't want the hassle of moving to the cell, getting the value, then moving back I have found the

STR_version = ActiveCell.Offset(-1, 0).Value

syntax very useful. You can get the value from a cell by offsetting whilst staying in the cell you are working on.

Roy


2018-03-01 05:58:40

Philip

Upper RIGHT cell ??


2013-08-20 13:52:34

Bryan

> Of course, Bryan, the title of the tip is "Relative VBA Selections," not "Relative VBA Ranges."

And? That doesn't stop you from saying "here's why you shouldn't do it". You have plenty of articles where the title doesn't dictate the content.

The way I see it, you can be an OK, Good, or Great teacher. An OK teacher answers the question as asked, even if it's not a good question ("How do I select a range relative to another range?"). A Good teacher answers the question that should have been asked, even if that's not what the student asked ("How do I *use* a range relative to another range?"). A Great teacher answers the question the student didn't even know they had ("What is the best way to interact with ranges?" -- maybe that's not a great example, but I don't claim to be a great teacher, either). This article is OK.

> VBA allows you to create either selections or ranges. Each have pros and cons.

Well, what are the pros and cons? They aren't located in another ExcelTips article; I checked. I've never read anything anywhere (but here) that claims that the Selection object is on par with the Range object. Here are 6 articles that agree:
* http://msdn.microsoft.com/en-us/library/office/aa141477(v=office.10).aspx "Once you get a solid grasp on the most efficient way to work with Excel objects, you will find yourself rewriting or restructuring the VBA code written by the macro recorder to use the Range object instead [of the Selection object]"
* http://www.avdf.com/apr98/art_ot003.html "Don't select sheet objects to use them"
* http://vbatips.com/2007/12/05/cleaning-up-recorded-macro-code/ "One terribly inefficient yet common habit to avoid is using the Select method in conjunction with Selection."
* http://www.cpearson.com/excel/optimize.htm "Generally, it is not necessary to select a range before working with it."
* http://msdn.microsoft.com/en-us/library/office/aa164764(v=office.10).aspx "most things you can do with a Selection object, you can do even faster with a Range object" (this technically applies to Word, but the principal is exactly the same)
* http://www.excelitems.com/2010/12/optimize-vba-code-for-faster-macros.html "Using too many Select and Selection effects (sic) the performance drastically"
* http://excelribbon.tips.net/T009329_Running_Macros_on_Hidden_Worksheets.html Technically, you don't promote Selection objects on this tip. However, it's pointed out in the comments that the entire tip is nullified by using Range objects instead, thus showing that the Range object is the way to go.

> This particular tip focuses on selections; other tips focus on ranges.

There are actually two tips that tell you how to find a range relative to a selection (this and T011402 -- interestingly, that one uses the single-line approach I suggest). I could find no articles about relative range objects, and I'd wager (there's no easy way for me to get the numbers to be sure) that the vast majority of articles here use Selection objects when they should be using Range objects (T008774, T011333, and T011169 are all examples of code that uses the Selection object when the Range object would be superior).

> It is up to the individual to determine which is best for their needs.

As this is a tips site, shouldn't it be up to the teacher to show how one can determine which is best for their needs?

> Even if one chooses to use ranges 99% of the time, it is still good to know how to create selections--particularly selections relative to the currently selected cell.

But it's really not, as I've shown. If you HAVE to teach selections, all you need to show is the .Select Method; the Offset and Resize properties will work on any Range object (of which the Selection object is one), so you can broaden the scope and still teach the same lesson.


2013-08-19 15:50:36

awyatt

Of course, Bryan, the title of the tip is "Relative VBA Selections," not "Relative VBA Ranges."

VBA allows you to create either selections or ranges. Each have pros and cons. This particular tip focuses on selections; other tips focus on ranges. It is up to the individual to determine which is best for their needs.

Even if one chooses to use ranges 99% of the time, it is still good to know how to create selections--particularly selections relative to the currently selected cell.

-Allen


2013-08-19 15:47:04

Bryan

Bah, more selecting! The only time I've ever needed code that selects a range is when using the ActiveWindow.FreezePanes property; everything else can be done without the slow, cumbersome, error-prone Selection object. (I do occasionally use the Selection object as an *input* to a macro, because this is better than hard-coding and more convenient then prompting every time).

Respectively, this is how I would write all three snippets (assuming you are going to do something else with the rng variable later):

Set rng = Selection.Offset(2,1)

With Selection
Set rng = Range(.Offset(2,1),.Offset(5,3))
End With

Set rng = Selection.Offset(2,1).Resize(4,3)


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.