ThaBillerSite.com
    Our Family Website

Tips, Tricks & How-To

  


Latest Tips

 Saturday, December 02, 2006


Excel Tip: Using the Selected Cell in a Formula   

Here's an interesting Excel problem.  Suppose you wanted to display the contents of the currently selected cell in another cell, or use the value from the selected cell in a formula.  Is this possible?

Yes, and no.  It is not part of excel's standard functionality, but it is possible with a little VBA.

  1. Create a named range on the cells you will be selecting.  We'll need this because if we select a cell outside this range, we don't need/want it to do anything.  Name it "SelectedValues" or similar, remember the name because we'll use it in step 3.
  2. Create a named range on an unused cell somewhere out of site.  This cell will hold a necessary temporary value.  Name this cell "selected".
  3. Paste the following code into the Visual Basic Editor (under Tools | Macro, or ALT+F11).  Make sure you select the appropriate code file for your worksheet (Sheet1 by default).
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
     If Not Intersect(Target, Range("SELECTEDVALUES")) Is Nothing Then
          Range("selected").Formula = "=" &Target.Address
     Else
          Range("selected").Formula = ""
     End If
    End Sub
    
    Change Range("SELECTEDVALUES"), and Range("selected") to the names you created in steps 1 & 2.

Now the value of the selected cell can be retrieved using the 'selected' named range. You can get the value of the selected cell by simply using the word 'selected' in a formula. (eg: =selected, or =sqrt(selected) ).

Download the example spreadsheet (.xls 31kb)
Note: You'll need to turn your macro security to medium or low (Tools | Options | Security | Macro Security)



jeremy at 11:45 PM | (0) Comments | Add a comment | Permalink