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.
by Allen Wyatt
(last updated August 17, 2013)
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:
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.
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!
Sometimes a macro command line can get very, very long. This can make it hard to understand when you look at it a month or so ...Discover More
Want to have you macro completely hide the Excel interface? You can do so by using the Visible property for the Excel ...Discover More
When you use a macro to process data you always run the risk of making that data unusable by Excel. This is especially true ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.