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 Cell in the Current Row.
by Allen Wyatt
(last updated October 17, 2015)
If you are developing Excel macros, you may wonder how you can select a cell relative to the one in which you are located. For instance, if you are using Excel and you press the Home key, the cell at the left side of the current row is selected. Unfortunately, using the macro recorder to record this does not help in this situation since it records destination of the action, instead of the your actual action. For instance, if you press Home and you are on the fourth row in a worksheet, Excel doesn't record the Home action, but instead records the destination, as follows:
This is great if you always want to go to cell A4, but terrible if you want to go to the first cell of whatever row you are on.
As with many tasks in VBA, there are several ways you can approach a solution to this dilemma. The first method is actually a variation on what the macro recorder returns, as shown above. All you need to do is change the row designator so it represents the current row, as in the following:
Range("A" & (ActiveCell.Row)).Select
VBA figures out what the current row is, slaps it together with the "A" designator, and comes up with a cell reference that works with the Range method.
Another technique you can use is to put the Cells property to work, as follows:
This approach, of course, can be modified so that you actually select any given cell in the current row. All you need to do is change the column designation (1, in the above example) to a number representing the column desired.
Another approach (which produces the same result) is to use the Range object in conjunction with the Cells property, as shown here:
Selection.Row gives the row number of the current selection. The Address property of the Cells method returns the address of a particular cell in A$1$ format. This address is then used as the parameter for the Range object, and the actual cell is selected by the Select method.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11440) 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 Cell in the Current Row.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
A common task for macros is to open and process a file you want imported into your workbook. If you need to identify the ...Discover More
If you have a series of values in a column, you might have a need to separate the values into even values and odd values. ...Discover More
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
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.