2 if statements into 1?

lgroulx2000

Board Regular
Joined
Jan 6, 2009
Messages
100
Office Version
  1. 365
Platform
  1. Windows
Hi,

How do I write these 2 if statments into 1?

If Not (Intersect(Target, Range(Cells(4, 2), Cells(62, 30))) Is Nothing) Then
CalendarFrm.Show
End If

If Not (Intersect(Target, Range(Cells(64, 2), Cells(69, 30))) Is Nothing) Then
CalendarFrm.Show
End If

Thanks,

Larry
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Just a thought but did you try...

If Not (Intersect(Target, Range(Cells(4, 2), Cells(62, 30))) Is Nothing) Or If Not (Intersect(Target, Range(Cells(64, 2), Cells(69, 30))) Is Nothing)
 
Upvote 0
Thanks,

I tried that but I get "expected: expression" after the Or.

Larry
 
Upvote 0
Maybe something like this?, but I did not test

Code:
If Not (Intersect(Target, Range(Cells(4, 2), Cells(62, 30))) Is Nothing) Then
CalendarFrm.Show
elseIf Not (Intersect(Target, Range(Cells(64, 2), Cells(69, 30))) Is Nothing) Then
CalendarFrm.Show
End If
 
Last edited:
Upvote 0
Code:
If Not ((Intersect(Target, Range(Cells(4, 2), Cells(62, 30))) Is Nothing)) OR (Not (Intersect(Target, Range(Cells(64, 2), Cells(69, 30))) Is Nothing)) Then
    CalendarFrm.Show
End If

or

Code:
If Not ((Intersect(Target, Range(Cells(4, 2), Cells(62, 30))) Is Nothing) AND  (Intersect(Target, Range(Cells(64, 2), Cells(69, 30))) Is Nothing)) Then
    CalendarFrm.Show
End If

Code:
If Not Application.Intersect(Target, Range("B4:AD62,B64:AD69")) Is Nothing Then
    CalendarFrm.Show
End If
 
Upvote 0
If its my code that is giving the error, its probably the wrong number of parenthesis.

The thought behind my first two is that you have
Code:
If Not (A) Then
    CalendarFrm.Show
End If
If Not (B) Then
    CalendarFrm.Show
End If
Since Not(A) and Not(B) are mutualy exclusive, we don't have to worry about the mixed case and can combine that to
Code:
If Not(A) Or Not(B) Then
    CalendarFrm.Show
End If
which is equivilant to
Code:
If Not (A And B) Then
    CalendarFrm.Show
End If

The last bit of code is noting that you want the calandar to show if Target is in one of the two ranges. I combined the ranges and wrote a test to see if Target is in that combined range.
 
Upvote 0
Yet another slight variation:
Code:
Dim myTarget As Range

Set myTarget = Union(Range(Cells(4, 2), Cells(62, 30)), _
                    Range(Cells(64, 2), Cells(69, 30)))
If Not Intersect(Target, myTarget) Is Nothing Then
    CalendarFrm.Show
End If
 
Upvote 0

Forum statistics

Threads
1,214,628
Messages
6,120,618
Members
448,973
Latest member
ChristineC

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