Written by Allen Wyatt (last updated August 18, 2018)
This tip applies to Excel 97, 2000, 2002, and 2003
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:
Range("A4").Select
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:
Cells(Application.ActiveCell.Row, 1).Select
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:
Range(Cells(Selection.Row, 1).Address).Select
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.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2267) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) 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!
Need to know the current hour of the day? You can derive the information in your macros by using the Hour function, as ...
Discover MoreWant to run a macro in Excel, but not sure if doing so will tie up your computer? Here's how macro processing really happens.
Discover MoreWhen processing a worksheet with a macro, it may be helpful to periodically recalculate the worksheet. Wouldn't it be ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2019-10-10 13:15:02
Tony Scocozza
Thanks for the tips Allen.
Is is possible to select a cell, which is a calculated value located in column "X", based on the formula value of another cell in the same worksheet?
(i.e. the source cell is something like "=MIN(X220:X1406)" and the destination cells in column "X" also contain formula based values like:
"=(X218+W219)-U219"
2019-03-29 19:52:17
Mindy
I have a table that uses dependent drop down lists to fill a cell that I would like to reset when the original cell is changed. This is easy to do for a specific row, but I'm unable to make it work for any row.
my current (working) code is this:
If Target.Address = Range("f3").Address Then
Range("f3:i3").Offset(, 1).Value = ""
End If
Thanks for any help.
2019-01-25 05:51:19
@Barry
Hi
I am not quite sure what point you are making?...
I think we are/ were talking mostly about coding to select a cell or cells on the row of where the current selection is. You are talking about a selection offset from where the current selection is.
Nothing wrong with what you are suggesting, but it is doing something slightly differen from what the Blog here is mostly discusing.
Depends what you want to do.
Alan Elston
2019-01-24 03:28:51
Surely the easiest and most obvious way is
Selection.Offset(0, xx).Select where xx is the number of cells to the right (used - for to the left) you wish the Selection to change by.
2019-01-23 03:21:39
Hi Zack J
Maybe something like this
Range("A" & ActiveCell.Row & ":G" & ActiveCell.Row & "").Select
Alan Elston
2019-01-22 15:02:12
Zack J
These are a great help. My only question is how would I apply this to select multiple cells. For example I would like to select cells A through G of the current row I am in. Any help would be greatly appreciated.
2018-10-15 10:40:30
Padmasai
Very useful tips on and explanation! Thank you!
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2023 Sharon Parq Associates, Inc.
Comments