2 if statements into 1?

lgroulx2000

Board Regular
Joined
Jan 6, 2009
Messages
100
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
 

Some videos you may like

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

jeffreybrown

Well-known Member
Joined
Jul 28, 2004
Messages
5,145
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)
 

conny2254

Board Regular
Joined
Jun 9, 2009
Messages
248
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:

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,904

ADVERTISEMENT

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
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,904

ADVERTISEMENT

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.
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
47,926
Office Version
  1. 365
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,122,487
Messages
5,596,450
Members
414,068
Latest member
FAH

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
Top