Solved

Excel VBA - find out if activecell is Named Range

Posted on 2011-03-05
4
668 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now