Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. 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.
Written by Allen Wyatt (last updated October 23, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
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:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11402) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. 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.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
Got a workbook cluttered with all sorts of macros? Delete them and you'll make your workbook easier to manage.
Discover MoreWant a quick way to jump to the end of your data entry area in a worksheet? The macro in this tip makes quick work of the ...
Discover MoreYou can easily use formulas to pull apart text stored in a cell. For instance, if you need to pull individual characters ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments