How to write If(Or(And(),And()) in VBA

MCTampa

Board Regular
Joined
Apr 14, 2016
Messages
97
[TABLE="width: 500"]
<tbody>[TR]
[TD]Start
[/TD]
[TD]End
[/TD]
[TD][/TD]
[TD]SD
[/TD]
[TD]ED
[/TD]
[TD]Check
[/TD]
[/TR]
[TR]
[TD]7/20/2019
[/TD]
[TD]7/27/2019
[/TD]
[TD][/TD]
[TD]8/6/2019
[/TD]
[TD]8/12/2019
[/TD]
[TD]False
[/TD]
[/TR]
[TR]
[TD]7/27/2019
[/TD]
[TD]8/3/2019
[/TD]
[TD][/TD]
[TD]8/6/2019
[/TD]
[TD]8/12/2019
[/TD]
[TD]False
[/TD]
[/TR]
[TR]
[TD]8/3/2019
[/TD]
[TD]8/10/2019
[/TD]
[TD][/TD]
[TD]8/6/2019
[/TD]
[TD]8/12/2019
[/TD]
[TD]True
[/TD]
[/TR]
[TR]
[TD]8/10/2019
[/TD]
[TD]8/17/2019
[/TD]
[TD][/TD]
[TD]8/6/2019
[/TD]
[TD]8/12/2019
[/TD]
[TD]True
[/TD]
[/TR]
[TR]
[TD]8/17/2019
[/TD]
[TD]8/24/2019
[/TD]
[TD][/TD]
[TD]8/6/2019
[/TD]
[TD]8/12/2019
[/TD]
[TD]False
[/TD]
[/TR]
</tbody>[/TABLE]

I'm struggling with how to write the following formula in VBA:

=IF(OR((AND(D2>=A2,D2<=B2)),AND(E2>=A2,E2<=B2)),TRUE,FALSE)

This formula evaluates whether or not either the SD or theED falls between the Start Date and the End Date. I do not care which one satisfies thecriteria, I just want to know if one of them does.
I am struggling with the structure in VBA.
Thanks,
Mike
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Ostensibly:

Code:
If (Range("A2")<=Range("D2") And Range("D2")<=Range("B2")) Or (Range("A2")<=Range("E2") And Range("E2")<=Range("B2")) Then
    '... SD or ED falls between Start and End
End If

However, that is efficient because VBA goes to Excel for each and every Range reference; it does not stop the left-to-right evaluation as soon as it can.

Somewhat more efficient:

Code:
Dim v as Variant
v = Range("A2:D2")
If (v(1,1)<=v(1,3) And v(1,3)<=v(1,2)) Or (v(1,1)<=v(1,4) And v(1,4)<=v(1,2)) Then
    '....
End If
 
Last edited:
Upvote 0
Or

Code:
Sub InsertFormula()
    With Range("F2:F6")
        .Formula = "=IF(OR(AND(D2>=A2,D2<=B2),AND(E2>=A2,E2<=B2)),TRUE,FALSE)"
        .Value = .Value
    End With
End Sub

M.
 
Upvote 0

Forum statistics

Threads
1,226,739
Messages
6,192,740
Members
453,755
Latest member
IQBS

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