
Tips.Net > ExcelTips Home > Macros > VBA Examples > Displaying the Selected Cell's Address
Summary: Do you want a cell to contain the address of whatever cell is currently selected in the workbook? There are a couple of ways you can achieve this goal, each involving the use of a macro. This tip demonstrates two techniques that update the cell contents in different ways. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003.)
Excel allows you to easily see the location of the currently selected cell by examining the contents of the Name Box, to the left of the Formula Bar. This is fine and good, but there are times when you would like to have the address of a cell actually in a cell. For instance, you may want cell A1 to contain the address of the currently selected cell. This means that if cell E4 were selected, then A1 would contain its address, or $E$4. If you then pressed the right-arrow key, then the contents of A1 would change to $F$4.
In order to return the address of the currently selected cell, you must resort to using macros. The following macro will return the value of the cell selected at the time it is run:
Public Function CurrentCell() As String
Application.Volatile
CurrentCell = ActiveCell.Address
End Function
The inclusion of the Application.Volatile method means that every time the worksheet is recalculated, this function (macro) is again run. To use the macro you can place the following in any cell desired, including A1:
=CurrentCell
You should note that this macro doesn't result in the contents of A1 changing every time you move to a different cell. Again, the contents of A1 will change only when the workbook is recalculated, either by changing something in the worksheet or by pressing F9.
If, instead, you need to have a "real time" version that automatically updates A1 as the selected cell is changed, you can follow these steps:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Range("A1").Value = ActiveCell.Address
End Sub
Now, as you move about this single sheet, the contents of A1 should be constantly updated to reflect your location.
Tip #2302 applies to Microsoft Excel versions: 97 2000 2002 2003
PivotTables Got You Perplexed? PivotTables for the Faint of Heart shows how you can start using Excel's PivotTable tool right away to spin your data into gold! You discover how easy it really is to crunch the numbers you need to crunch. Uncover the power of creating PivotTables, editing them, formatting them, customizing them, and much more.
Check out PivotTables for the Faint of Heart today!
If you have tons of data to analyze, one of the best tools in Excel's arsenal is the PivotTable. Learn how to use this tool to analyze your data. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pest Tips
Pet Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site