Worksheet_BeforeDoubleClick multiple ranges different events

nicnad

Board Regular
Joined
Sep 12, 2011
Messages
199
Hi,

I want to run different macro when different range are double clicked. I'm fine with two events with If and ElseIf but I want to add a third event with a different range. Something like this :

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
 
Cancel = True
Dim myRangedoc As Range
Set myRangedoc = Range("phonebookname")
If Not Intersect(Target, myRangedoc) Is Nothing Then
Macro1
 
ElseIf Not Intersect(Target, Range("rangefilter")) Is Nothing Then
If Not Intersect(Target, Range("Rangefilter")) Is Nothing Then
Macro2
 
ElseIf Not Intersect(Target, Range("rangedoc")) Is Nothing Then
If Not Intersect(Target, Range("rangedoc")) Is Nothing Then
Macro3
 
End If
End If
End If
 
End Sub
Macro1 and Macro 2 run when I click the appropriate range but Macro3 isn't running when I click in "rangedoc". I'm sure I am not using the Else If Not properly.

Can you please help me with this issue.

Thank you.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Untested, but try
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)   
Cancel = True 
Dim myRangedoc As Range 
Set myRangedoc = Range("phonebookname") 
If Not Intersect(Target, myRangedoc) Is Nothing Then 
Macro1   

ElseIf Not Intersect(Target, Range("rangefilter")) Is Nothing Then 
Macro2   

ElseIf Not Intersect(Target, Range("rangedoc")) Is Nothing Then 
Macro3  
End If   
End Sub
 
Upvote 0
You don't need the Ifs if you have ElseIf:

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
    Cancel = True
    Dim myRangedoc As Range
    Set myRangedoc = Range("phonebookname")
    If Not Intersect(Target, myRangedoc) Is Nothing Then
        Macro1
    ElseIf Not Intersect(Target, Range("rangefilter")) Is Nothing Then
        Macro2
    ElseIf Not Intersect(Target, Range("rangedoc")) Is Nothing Then
        Macro3
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,569
Messages
6,179,605
Members
452,928
Latest member
VinceG

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top