double click macros

  • Thread starter Thread starter jack
  • Start date Start date
J

jack

Can someone help me on how I can set up two different double click macros
(two different resulting actions) on the same worksheet for different
ranges. I have each macro starting with: Private Sub
Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean). I
found very quickly that it doesn't work. What do I need to do so that each
will work independently?
Any basic direction would be appreciated.
 
Hi Jack,
You can just use code like this..

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel
As Boolean)
If Not Intersect(Target, Range("ABC")) Is Null Then
'do something here
ElseIf Not Intersect(Target, Range("DEF")) Is Null Then
'do something else here
End If
End Sub

Alok
 
Maybe something like this
Private Sub Worksheet_BeforeDoubleClick _
(ByVal Target As Range, Cancel As Boolean)
Set Target = Range("A1")
If Target.Address = ActiveCell.Address Then
MsgBox "yes"
Else
Set Target = Range("A2")
If Target.Address = ActiveCell.Address Then
MsgBox "yes"
Else
MsgBox "no"
End If
End If
End Sub
 
Private Sub Worksheet_BeforeDoubleClick _
(ByVal Target As Range, Cancel As Boolean)
Set Target = Range("A1")
If Target.Address = ActiveCell.Address Then
MsgBox "Run Macro 1"
Else
Set Target = Range("A2")
If Target.Address = ActiveCell.Address Then
MsgBox "Run Macro 2"
Else
Set Target = Range("A3")
If Target.Address = ActiveCell.Address Then
MsgBox "Run Macro 3"
End If
End If
End If
End Sub
 
Here's what doesn't work. How do I correct It so both macros work on
different ranges? The error message I get is: Ambiguous name detected
They both work independently but not on the same project.

Option Explicit

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As
Boolean)

Application.EnableEvents = False
On Error GoTo sub_exit
If Not Intersect(Target, Range("c7:c57")) Is Nothing Then
With Target
If .Value = "X" Then
.Value = ""
Else

.Value = "X"

End If
End With
Cancel = True
End If
sub_exit:
Application.EnableEvents = True
End Sub
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As
Boolean)

Dim Rng1 As Range

'Assign the range to work with
Set Rng1 = Range("D7:D57")

'Only work on assigned range
If Intersect(Target, Rng1) Is Nothing Then Exit Sub

'Cancel cell editing that would normally trigger when you double click
Cancel = True

'Call the userform
UserForm1.Show

End Sub
 
Try this
Private Sub Worksheet_BeforeDoubleClick _
(ByVal Target As Range, Cancel As Boolean)

If Not Intersect(Target, Range("c7:c57")) Is Nothing Then
With Target
If .Value = "X" Then
.Value = ""
Else
.Value = "X"
End If
End With
Cancel = True
End If
'Assign the range to work with
'Only work on assigned range
If Intersect(Target, Range("D7:D57")) Is Nothing Then Exit Sub

'Cancel cell editing that would normally trigger when you double click
Cancel = True

'Call the userform
UserForm1.Show




End Sub
 
Thanks Mike, that worked for me


Mike said:
Try this
Private Sub Worksheet_BeforeDoubleClick _
(ByVal Target As Range, Cancel As Boolean)

If Not Intersect(Target, Range("c7:c57")) Is Nothing Then
With Target
If .Value = "X" Then
.Value = ""
Else
.Value = "X"
End If
End With
Cancel = True
End If
'Assign the range to work with
'Only work on assigned range
If Intersect(Target, Range("D7:D57")) Is Nothing Then Exit Sub

'Cancel cell editing that would normally trigger when you double click
Cancel = True

'Call the userform
UserForm1.Show




End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top