Selecting the Named Range that Contains the Active Cell in Excel 2007

Microsoft Excel, Tips & Tricks Add comments

msexceltweak.pngIt’s often handy to be able to select the name range that contains the current cell (for exam­ple, to change the range formatting). If you know the name of the range, you need only select it from the Name box. However, in a large model or a workbook that you’re not familiar with, it may not be obvious which name to choose. Listing 1 shows a VBA func­tion and procedure that handles this chore for you.

Listing 1 A VBA Function and Procedure That Determines and Selects the Named Range Containing the Active Cell
A VBA Function and Procedure That Determines and Selects the Named Range Containing the Active Cell

The heart of Listing 1 is the GetRangeName function, which takes a range as an argument. The purpose of this function is to see if the passed range-r-is part of a named range and if so, to return the name of that range. The function’s main loop runs through each item in the active workbook’s Names collection. For each name, the RefersToRange property returns the associated range, which the function stores in the rtr variable. The function then uses the Intersect method to see if the ranges r and rtr intersect. If they do, it means that r is part of the named range (because, in this case, r is just a single cell), so GetRangeName returns the range name. If no intersection is found for any name, the function returns the null string (”") instead.

The SelectCurrentNamedRange procedure makes use of the GetRangeName function. The procedure stores the active cell in the r variable and then passes that variable to the GetRangeName function. If the return value is not the null string, the procedure selects the returned range name.

Technorati Tags: , ,

Popularity: 2% [?]

If you liked this post, would you please buy me a twelve-ounce lattes for only $2

Related Post

  • Selecting A1 on All Worksheets in Excel 2007
  • Selecting the “Home Cell” on All Worksheets in Excel 2007
  • Creating a Scrolling Chart in Excel 2007
  • Allowing Only Certain Values in a Cell
  • Charting a Dynamic Range in Excel 2007
  • Don't Find What You're Looking For? Please Try Here...

    Google
     

    Leave a Reply

    WP Theme & Icons by N.Design Studio
    Entries RSS Comments RSS Add to Technorati Favorites Log in