adding restrictions on action Double_Click

  • Thread starter Thread starter Junior Trimon
  • Start date Start date
J

Junior Trimon

The following code gives an action for double clicking on
a cell in a sheet.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As
Excel.Range, Cancel As Boolean)
MsgBox "You double clicked on cell " & Target.Address
Cancel = True
End Sub

Now I want to add a restriction with this action.
I want this code to work only for a specific range on the
sheet.

Is this possible?
 
Using John Walkenbach's InRange function from
http://j-walk.com/ss/excel/tips/tip64.htm, you can do:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim oAllowableRange As Range
Set oAllowableRange = Sheet1.Range("A1:C3")
If InRange(Target, oAllowableRange) Then
MsgBox "That's fine."
Else
MsgBox "You're out of range."
Cancel = True
End If

End Sub

Function InRange(rng1, rng2) As Boolean
' Returns True if rng1 is a subset of rng2
InRange = False
If rng1.Parent.Parent.Name = rng2.Parent.Parent.Name Then
If rng1.Parent.Name = rng2.Parent.Name Then
If Union(rng1, rng2).Address = rng2.Address Then
InRange = True
End If
End If
End If
End Function
 
Place your code between this lines and it will only work
in the range A1:H20


If Not Application.Intersect(Range("A1:H20"), Target) Is Nothing Then


End If
 
Back
Top