Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Excel VBA - find out if activecell is Named Range

Posted on 2011-03-05
4
Medium Priority
?
689 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

618 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