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.
by Allen Wyatt
(last updated October 3, 2015)
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.
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.
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!
Macros are great at working with text. This tip presents an example that shows this versatility by reversing the contents ...Discover More
Excel allows you to add pictures to your worksheet, even within a macro. However, you might have a bit harder time ...Discover More
Does your macro need to change the width of some columns in a worksheet? Here's how to do it.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.