Solved

Excel VBA - find out if activecell is Named Range

Posted on 2011-03-05
4
676 Views
Last Modified: 2012-05-11
For a Worksheet_BeforeDoubleClick event I want to find out if the Target is a Named Range "ClickCell". I tried the Name property which gives Sheet1!$D$4. I want it to return "ClickCell".
Thanks.
0
Comment
Question by:hindersaliva
  • 2
4 Comments
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35042178
Is this what you want?

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim nm As Name
    
    For Each nm In ActiveWorkbook.Names
        If CheckRange(Target, Range(nm)) Then MsgBox nm.Name
    Next
End Sub

Function CheckRange(TargetToCheck As Range, NMToCheck As Range) As Boolean
    On Error Resume Next
    CheckRange = Not Application.Intersect(TargetToCheck, NMToCheck) Is Nothing
    Err.Clear: On Error GoTo -1: On Error GoTo 0
End Function

Open in new window


Sid
0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 35042581
You can of course do it as a one liner using:


intersect(range("ClickCell"), target) is nothing

Chris
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    
    If Intersect(Range("ClickCell"), Target) Is Nothing Then
        MsgBox "Not in range"
    Else
        MsgBox "Is in range"
    End If

End Sub

Open in new window

0
 

Author Closing Comment

by:hindersaliva
ID: 35042627
Perfect. Thanks Chris.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 35042735
Glad it helped, and thanks for the prompt response.

Chris
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
remove dups 10 39
Excel Cell Total 3 25
Excel filter on tab not showing any entries? 5 24
How to transform one row line like this in excel table 2010? 8 23
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question