Highlight Named Ranges Part 2

April 6, 2010 by datapig Leave a reply »

Last week, I posted 'Highlight All Named Ranges Part 1', where I shared a macro that will literally color all the cells that belong to any Named Range yellow.

This allows you to get a visual indication of how many Named Ranges exist and where they are.

 

For example, in this screenshot I've run my highlight ranges code. You can see there are four Named Ranges here.

This is pretty cool, but now I'd like to figure out exactly which Named Range each cell belongs to?

That is to say, I can now see that cell F3 belongs to a Named Range, but how do I figure out which range it belongs to?

Ideally, I'd like to double-click on Cell F3 and see the name of the Named Range.

 

 

This is where this brilliant piece of code comes in. This procedure will check the range of the double-clicked cell to see if it intersects with any of the existing Named Ranges. Then it returns an answer in the form of a message box.

Visual Basic:
  1. Dim oName As Name
  2. Dim sList As String
  3.  
  4. 'Check the active cell against each Named Range
  5.     For Each oName In ActiveWorkbook.Names
  6.        
  7. 'If the active cell intersects with a Named Range, capture the Name
  8.         If Not Intersect(ActiveCell, Range(oName.RefersTo)) Is Nothing Then
  9.             If sList = "" Then
  10.                 sList = oName.Name
  11.             Else
  12.                 sList = sList & vbCrLf & oName.Name
  13.             End If
  14.         End If
  15.     Next oName
  16.    
  17. 'Create a message if no Named Ranges were found
  18.     If sList = "" Then
  19.         MsgBox "This cell is not referenced in any Named Ranges"
  20.     Else
  21. 'If Named Ranges were found, show them in a message
  22.         MsgBox sList, vbOKOnly, "Referenced in: "
  23.     End If

 

To implement this as a double-click, simply paste the code into the BeforeDoubleClick event of the target worksheet.

 

 

With this code in place, I can now double click on Cell F3 and see exactly which Named Range It belongs to.

In this case, it looks like F3 belongs to the Data2009 Named Range.

 

 

If the active cell belongs to several named ranges, then they will all be listed.

 

 

So this piece of code, coupled with the code in 'Highlight All Named Ranges Part 1' makes for a pretty handy spreadsheet auditing combo.

 

Ok then – I gotta run. It's 5am here, and the kids are still sleeping. It's time to steal more candy from their Easter Baskets.

RELATED STUFF

  1. Mocking the ‘Merge & Center’ Icon
  2. Highlighting the Active Row and Column
  3. Protect Worksheet Structure with Array Formulas
  4. Using the Switch Function in Excel
  5. Why Replace the DOM Interface
  6. Running an Access Parameter Query from Excel
  7. Fun with Custom Lists
  8. Excel Defunct Defaults
  9. Why FIND when you can SEARCH
  10. Getting Data from Previous Sheet
Advertisement

7 Responses

  1. AlexJ says:

    datapig,
    I've been doing something similar with code borrowed from Rob Bovey's ExcelUtilities 2.1.01.

    I added a list box instead of a message, and allow the user to select one of the named ranges found. This highlights the full selected range. (Rob's code is the kool bit, though).

  2. Of course the Name Manager, By Charles Williams and myself ,has this capability built-in.
    http://www.jkp-ads.com/officemarketplacenm-en.asp

  3. m-b says:

    Off topic and maybe a dumb question but how do you change the colour of the code in the VBA editor so that it becomes easier to read (as you show in your code examples)?

  4. Rick Rothstein (MVP - Excel) says:

    @m-b: The VB Editor already assigns colors by default; however, you can change them by clicking Tools/Options on the VB Editor's menu bar and then selecting the Editor Format tab... pick your editor items one at a time and change the colors as desired.

  5. m-b says:

    @Rick: thanks, I didn't know about that. Are those settings saved somewhere so you can 'transport' them between computers?

  6. Rick Rothstein (MVP - Excel) says:

    @m-b... I don't know for sure, but my gut instinct is that, no, they are not saved somewhere that you can get to easily enough to transfer between computer (my bet is that they are stored in the registry somewhere).

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy this password:

* Type or paste password here:

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>