Home » excel » excel – BeforeDoubleClick and Intersect

excel – BeforeDoubleClick and Intersect

Posted by: admin April 23, 2020 Leave a comment


I’m losing my mind:

I have this code in my Sheet 2, and its working great (see image for code):

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Intersect(Target, Range("A5:D150")) Is Nothing Then Cancel = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("A6:D150")) Is Nothing Then Exit Sub
    On Error GoTo ExitPoint
    Application.EnableEvents = False
    If Not IsDate(Target(1)) Then
        MsgBox " You can't delete cell content :)" _
        , vbCritical, "Sara"
    End If
    Application.EnableEvents = True
End Sub

Now I have an Excel sheet that is cells A:AC.

I want the formula applied to A:D, skip E, applied to F:V, skip W:Z, applied to AA:AC.

How can I implement these codes to multiple ranges?

I have tried many options and failed. I really appreciate any help and feedback.

How to&Answers:

You can either try to use Union. Something like:

Dim r As Range
Set r = Union(Range("A6:D150"),Range("F6:V150"),Range("AA6:AC150"))

If Intersect(Target, r) Is Nothing Then
'rest of your code

or you can directly use non-contiguous range notation:

If Intersect(Target, Range("A6:D150,F6:V150,AA6:AC150")) Is Nothing Then
'rest of your code