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: Relative VBA Selections.

Relative VBA Selections

by Allen Wyatt
(last updated August 17, 2013)

3

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

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (7016) applies to Microsoft Excel 2007, 2010, and 2013. 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

Quick AutoFill Variations

The AutoFill feature can be used for more than just incrementing information into cells. This tip explains how to access the ...

Discover More

Unwanted Page Breaks in Cross-References

You can use fields to cross-reference different text in a document. If you get unwanted results from the cross-referencing ...

Discover More

Replacing Spaces in Part Numbers with Dashes

Word has a power capability to search for information and then replace that information in some way. Finding the right method ...

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

Maximum Length Limit for a Macro

Make your macros too long, and Excel may just refuse to run them at all. This tip explains what the limit is for macros, and ...

Discover More

Automating the Importing of Macros

Macros are great when it comes to automating how you work with your workbooks. What if you want to fundamentally change the ...

Discover More

Changing the Default Drive

Do you have a macro that need to read and write files? If so, then there is a good chance you need to specify the default ...

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

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.