Conditional Logic VBA

SBF12345

Well-known Member
Joined
Jul 26, 2014
Messages
614
I am using the line below to determine characteristic qualities of a set of values. How is this being read? I would like for the test to meet the first part of the test and at least one of the second characteristics defined by the OR statement and contained within parenthesis.

Code:
If Cells(kk, 3) < Cells(hh, 3) And (StartDate2 <= StartDate1 <= EndDate2 Or StartDate2 <= EndDate1 <= EndDate2) Then
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
i cant test this against data but something like

=If(AND(cells(kk,3)<cells(hh,3),OR(startdate2 <=startdate1,startdate1 <=endate2,enddat2

so AND checks the first statement (cells(kk,3)<cells(hh,3), against the second. The OR(startdate2 <=startdate1,startdate1 <=endate2,enddat2 looks at the alternate options

this is not the answer, just something to work with
 
Upvote 0
You are trying to evaluate a boolean expression of the form

Condition1 AND (Condition2 OR condition 2)

Where

Condition1 = Cells(kk, 3) < Cells(hh, 3)
Condition2 = StartDate2 <= StartDate1 <= EndDate2
Condition3 = StartDate2 <= EndDate1 <= EndDate2

The problem is that you are constructing condition2 and condition3, assuming that VBA will chain boolean expressions like C/C++ will, and in my experience, VBA dislikes this. You need to break up condition2 & 3 so that you are only testing two elements at a time, e.g. Condition2 = (StartDate2 <= StartDate1) OR <= (StartDate1 <= EndDate2)
 
Upvote 0
I wrote it to read as below, but am receiving a "must be first statement on the line" compile error on the line directly below

Code:
Else If StartDate2 <= EndDate1 And EndDate1 <= EndDate2 AND Cells(kk,3) < Cells(hh,3) Then

Code:
If StartDate2 <= StartDate1 And StartDate1 <= EndDate2 And Cells(kk, 3) < Cells(hh, 3) Then        
            Cells(hh, 3).Interior.ColorIndex = 5
        
            Else If StartDate2 <= EndDate1 And EndDate1 <= EndDate2 AND Cells(kk,3) < Cells(hh,3) Then
        
            Cells(hh, 3).Interior.ColorIndex = 5
            
        End If
 
Upvote 0
I broke the two statements into two separate IF statements and it seems to be working. Thanks for your help!
 
Upvote 0
Else If should be one word i.e. ElseIf
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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