bottom
Great ExcelTips!
         
Your e-mail address is safe!
Close Note

Tips.Net > ExcelTips Home > Macros > VBA Examples > Displaying the Selected Cell's Address

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:

  1. Display the VBA Editor by pressing Alt+F11.
  2. In the Project window, at the left side of the Editor, double-click on the name of the worksheet you are using. (You may need to first open the VBAProject folder, and then open the Microsoft Excel Objects folder under it.)
  3. In the code window for the worksheet, click on the Object drop-down list and choose Worksheet. When you do, the Procedure should change to SelectionChange, and the framework for the event handler should appear in the code window.
  4. Change the event handler so it appears as follows:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
    Range("A1").Value = ActiveCell.Address
End Sub
  • Close the VBA Editor.
  • 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!

    Helpful Links

    Ask an Excel Question
    Make a Comment

    Tips.Net Home

    ExcelTips FAQ
    ExcelTips Premium

    Learn Access Now

    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

     

    Great Info!

    Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your e-mail address and click "Subscribe."
         
    (Your e-mail address will never be shared with anyone, ever.)