Multiple if intersect(target, range... with worksheet selection change

Chewyhairball

Active Member
Joined
Nov 30, 2017
Messages
312
Office Version
  1. 365
Platform
  1. Windows
Hi

Is it possible to get multiple
VBA Code:
If Intersect(Target, Range("my range")) Is Nothing Or Target.Cells.Count > 1 Then Exit Sub
within a worksheet selection change event?

I have the following but which ever one if first is the only one that works.

VBA Code:
If Intersect(Target, Range("H10:H2000")) Is Nothing Or Target.Cells.Count > 1 Then Exit Sub
MsgBox "my message"

If Intersect(Target, Range("K10:K2000")) Is Nothing Or Target.Cells.Count > 1 Then Exit Sub
Call mymacro
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
I would restructure it like this:
VBA Code:
Target.Cells.Count > 1 Then Exit Sub

If Not Intersect(Target, Range("H10:H2000")) Is Nothing Then
    MsgBox "my message"
Else
    If Not Intersect(Target, Range("K10:K2000")) Is Nothing Then
        Call mymacro
    End If
End If
 
Upvote 0
Solution
I would restructure it like this:
VBA Code:
Target.Cells.Count > 1 Then Exit Sub

If Not Intersect(Target, Range("H10:H2000")) Is Nothing Then
    MsgBox "my message"
Else
    If Not Intersect(Target, Range("K10:K2000")) Is Nothing Then
        Call mymacro
    End If
End If
Cool! I’ll give it a go when I’m back at my PC and let you know 🙂
 
Upvote 0
another way maybe

VBA Code:
If Target.Cells.Count > 1 Then Exit Sub
    
    With Me.Range("H10:H2000,K10:K2000")
        If Not Intersect(Target, Me.Range(.Address)) Is Nothing Then
            If Not Intersect(Target, .Areas(1)) Is Nothing Then MsgBox "my message" Else MyMacro
        End If
    End With

Dave
 
Upvote 0
Thanks guys. Both worked perfect and iv gone with Joe4's. Both will be stored in my useful macros folder though :)

thanks again
 
Upvote 0
You are welcome.
Glad we could help!
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,517
Members
449,088
Latest member
RandomExceller01

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