How to solve a issue with three criterias

most

Board Regular
Joined
Feb 22, 2011
Messages
107
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. Mobile
Hi,
I would like some input/pointers on how to solve this, I'm not sure where to begin...
I want to write Active, Not active or Ignore in column E based on three criteria.

As you can see in the table, a person can occur 2-3 times, even 6 times in my orginal data. Data is sorted on column A.

If same employment (column B) occurs two times = all rows should be Ignore.
If employment (column B) differs then check if startdate (column C) and enddate (column D) is within range of "orange date" (B17) then Active else Not active.

/Marcus

ABCDE
Expected result
Active
Not active
Ignore
Ignore
Ignore
Ignore
Ignore
Active
Not active
Not active

<tbody>
[TD="align: center"]1[/TD]
[TD="bgcolor: #00B050"]Person[/TD]
[TD="bgcolor: #00B050"]Employment[/TD]
[TD="bgcolor: #00B050"]StartDate[/TD]
[TD="bgcolor: #00B050"]EndDate[/TD]

[TD="align: center"]2[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C6E0B4]#C6E0B4[/URL] , align: right"]194505[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C6E0B4]#C6E0B4[/URL] , align: right"]02[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C6E0B4]#C6E0B4[/URL] , align: right"]2019-04-24[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C6E0B4]#C6E0B4[/URL] , align: right"]2019-11-01[/TD]

[TD="align: center"]3[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C6E0B4]#C6E0B4[/URL] , align: right"]194505[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C6E0B4]#C6E0B4[/URL] , align: right"]01[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C6E0B4]#C6E0B4[/URL] , align: right"]2013-06-17[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C6E0B4]#C6E0B4[/URL] , align: right"]2019-03-29[/TD]

[TD="align: center"]4[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CDFFCD]#CDFFCD[/URL] , align: right"]184810[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CDFFCD]#CDFFCD[/URL] , align: right"]02[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CDFFCD]#CDFFCD[/URL] , align: right"]2016-01-01[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CDFFCD]#CDFFCD[/URL] , align: right"]2019-10-01[/TD]

[TD="align: center"]5[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CDFFCD]#CDFFCD[/URL] , align: right"]184810[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CDFFCD]#CDFFCD[/URL] , align: right"]02[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CDFFCD]#CDFFCD[/URL] , align: right"]2016-01-01[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CDFFCD]#CDFFCD[/URL] , align: right"]2019-10-01[/TD]

[TD="align: center"]6[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C6E0B4]#C6E0B4[/URL] , align: right"]175007[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C6E0B4]#C6E0B4[/URL] , align: right"]03[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C6E0B4]#C6E0B4[/URL] , align: right"]2019-07-23[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C6E0B4]#C6E0B4[/URL] , align: right"]2019-12-31[/TD]

[TD="align: center"]7[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C6E0B4]#C6E0B4[/URL] , align: right"]175007[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C6E0B4]#C6E0B4[/URL] , align: right"]02[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C6E0B4]#C6E0B4[/URL] , align: right"]2018-01-01[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C6E0B4]#C6E0B4[/URL] , align: right"]2019-06-30[/TD]

[TD="align: center"]8[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C6E0B4]#C6E0B4[/URL] , align: right"]175007[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C6E0B4]#C6E0B4[/URL] , align: right"]02[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C6E0B4]#C6E0B4[/URL] , align: right"]2018-01-01[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=C6E0B4]#C6E0B4[/URL] , align: right"]2019-06-30[/TD]

[TD="align: center"]9[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CDFFCD]#CDFFCD[/URL] , align: right"]166109[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CDFFCD]#CDFFCD[/URL] , align: right"]05[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CDFFCD]#CDFFCD[/URL] , align: right"]2019-03-11[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CDFFCD]#CDFFCD[/URL] , align: right"]2019-12-31[/TD]

[TD="align: center"]10[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CDFFCD]#CDFFCD[/URL] , align: right"]166109[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CDFFCD]#CDFFCD[/URL] , align: right"]04[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CDFFCD]#CDFFCD[/URL] , align: right"]2019-01-01[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CDFFCD]#CDFFCD[/URL] , align: right"]2019-03-10[/TD]

[TD="align: center"]11[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CDFFCD]#CDFFCD[/URL] , align: right"]166109[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CDFFCD]#CDFFCD[/URL] , align: right"]03[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CDFFCD]#CDFFCD[/URL] , align: right"]2018-09-10[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CDFFCD]#CDFFCD[/URL] , align: right"]2018-12-31[/TD]

[TD="align: center"]12[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]13[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]14[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]15[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]16[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]17[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FFC000"]2019-09-19[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

</tbody>
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hi, I get a different result for row 6 than you expect - but maybe you can try:


Excel 2013/2016
ABCDE
1PersonEmploymentStartDateEndDateExpected result
2194505224/04/201901/11/2019Active
3194505117/06/201329/03/2019Not Active
4184810201/01/201601/10/2019Ignore
5184810201/01/201601/10/2019Ignore
6175007323/07/201931/12/2019Active
7175007201/01/201830/06/2019Ignore
8175007201/01/201830/06/2019Ignore
9166109511/03/201931/12/2019Active
10166109401/01/201910/03/2019Not Active
11166109310/09/201831/12/2018Not Active
12
13
14
15
16
1719/09/2019
Sheet1
Cell Formulas
RangeFormula
E2=IF(COUNTIFS($A$2:$A$11,A2,$B$2:$B$11,B2)>1,"Ignore",IF(AND(C2<=$B$17,D2>=$B$17),"Active","Not Active"))
 
Upvote 0
Hi, I get a different result for row 6 than you expect - but maybe you can try:
[TABLE="width: 85%"]
<tbody>[TR]
[TD]Worksheet Formulas[TABLE="width: 100%"]
<tbody>[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]
[TH]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
[TR]
[TH="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]E2[/TH]
[TD="align: left"]=IF(COUNTIFS($A$2:$A$11,A2,$B$2:$B$11,B2)>1,"Ignore",IF(AND(C2<=$B$17,D2>=$B$17),"Active","Not Active"))[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]

Forget to mention, I prefer a VBA solution, but I got the same result as you and Business might actually accept this.
Thanks a lot!
If someone have some other idea, please share.
 
Upvote 0
Try

Excel Workbook
ABCDE
1PersonEmploymentStartDateEndDateExpected result
2194505224/04/20191/11/2019Active
3194505117/06/201329/03/2019Not Active
418481021/01/20161/10/2019Ignore
518481021/01/20161/10/2019Ignore
6175007323/07/201931/12/2019Ignore
717500721/01/201830/06/2019Ignore
817500721/01/201830/06/2019Ignore
9166109511/03/201931/12/2019Active
1016610941/01/201910/03/2019Not Active
11166109310/09/201831/12/2018Not Active
12
13
14
15
16
1719/09/2019
Activity
 
Upvote 0
Forget to mention, I prefer a VBA solution, ..
The vba could basically use the formula I suggested.
I have assumed that the "orange date" cell is a named range "FixedDate"

Code:
Sub Activity()
  Dim lr As Long
  
  lr = Range("D" & Rows.Count).End(xlUp).Row
  With Range("E2:E" & lr)
    .Formula = Replace(Replace("=IF(AGGREGATE(14,6,COUNTIFS($A$2:$A$#,A2,B$2:B$#,B$2:B$#),1)>1,""Ignore"",IF(MEDIAN(C2,D2,^)=^,""Active"",""Not Active""))", "#", lr), "^", Range("FixedDate").Address)
    .Value = .Value
  End With
End Sub
 
Upvote 0
Your "=IF(AGGREGATE(14,6,..." formula works flawless. Thanks!
But I identified a new example, EndDate can be blank were expected result should be Active.
My solution to that was to replace D2 with (IF(D2="";"2100-01-01";D2))

As you know, your VBA solution is only inserting the formula. I meant a more pure VBA solution which fills a text value in column E based on the criteria.
My original data has about 1000 lines, and with 8 threads this takes about 1-2 minutes to update, not a biggie but makes it kind of inconvenient for each update.
 
Upvote 0
I have now also found out that two rows can be ACTIVE, so I need to identify them some how.

I'm trying to solve this with formulas Match and Countifs, but I can't get it to work. I don't really understand how to use them...
Code:
=IF(MATCH(A2;A$2:A$1197;0);COUNTIFS(E$2:E$1197;"Active"))
 
Upvote 0
Your "=IF(AGGREGATE(14,6,..." formula works flawless. Thanks!
But I identified a new example, EndDate can be blank were expected result should be Active.
My solution to that was to replace D2 with (IF(D2="";"2100-01-01";D2))

As you know, your VBA solution is only inserting the formula. I meant a more pure VBA solution which fills a text value in column E based on the criteria.
My original data has about 1000 lines, and with 8 threads this takes about 1-2 minutes to update, not a biggie but makes it kind of inconvenient for each update.
Try this version
Code:
Sub Activity_v2()
  Dim d1 As Object, d2 As Object
  Dim a As Variant
  Dim i As Long
  Dim OrangeDate As Date
  
  Set d1 = CreateObject("Scripting.Dictionary")
  Set d2 = CreateObject("Scripting.Dictionary")
  a = Range("A2:E" & Range("A" & Rows.Count).End(xlUp).Row).Value
  OrangeDate = Range("FixedDate").Value
  For i = 1 To UBound(a)
    If d1.exists(a(i, 1) & "|" & a(i, 2)) Then
      d2(a(i, 1)) = Empty
    Else
      d1(a(i, 1) & "|" & a(i, 2)) = Empty
    End If
  Next i
  For i = 1 To UBound(a)
    If d2.exists(a(i, 1)) Then
      a(i, 5) = "Ignore"
    Else
      If IsEmpty(a(i, 4)) Then
        If OrangeDate >= a(i, 3) Then
          a(i, 5) = "Active"
        Else
          a(i, 5) = "Not Active"
        End If
      ElseIf OrangeDate >= a(i, 3) And OrangeDate <= a(i, 4) Then
        a(i, 5) = "Active"
      Else
        a(i, 5) = "Not Active"
      End If
    End If
  Next i
  Range("E2").Resize(UBound(a)).Value = Application.Index(a, 0, 5)
End Sub



I have now also found out that two rows can be ACTIVE, so I need to identify them some how.

I'm trying to solve this with formulas Match and Countifs, but I can't get it to work. I don't really understand how to use them...
Code:
=IF(MATCH(A2;A$2:A$1197;0);COUNTIFS(E$2:E$1197;"Active"))
I don't understand exactly what you want here. Could we have a small set of sample data with result(s) & any further clarification that you can give?
 
Upvote 0
Thanks, your scripts works flawless.
I've added two new use case, line 12-15. The script generates Active or Not active on both, but expected is Ignore on all four.

Excel 2016 (Windows) 32 bit
[Table="width:, class:head"][tr=bgcolor:#E0E0F0][th] [/th][th]
A
[/th][th]
B
[/th][th]
C
[/th][th]
D
[/th][th]
E
[/th][th]
F
[/th][th]
G
[/th][th]
H
[/th][th]
I
[/th][th]
J
[/th][/tr]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
1
[/td][td]Person[/td][td]Employment[/td][td]StartDate[/td][td]EndDate[/td][td][/td][td][/td][td=bgcolor:#00B050]Expected result[/td][td][/td][td][/td][td][/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
2
[/td][td]
194505​
[/td][td]02[/td][td]
2019-04-24​
[/td][td]
2019-11-01​
[/td][td]Active[/td][td][/td][td]Active[/td][td][/td][td][/td][td]
2019-09-19​
[/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
3
[/td][td]
194505​
[/td][td]01[/td][td]
2013-06-17​
[/td][td]
2019-03-29​
[/td][td]Not Active[/td][td][/td][td]Not active[/td][td][/td][td][/td][td][/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
4
[/td][td]
184810​
[/td][td]02[/td][td]
2016-01-01​
[/td][td]
2019-10-01​
[/td][td]Ignore[/td][td][/td][td]Ignore[/td][td][/td][td][/td][td][/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
5
[/td][td]
184810​
[/td][td]02[/td][td]
2016-01-01​
[/td][td]
2019-10-01​
[/td][td]Ignore[/td][td][/td][td]Ignore[/td][td][/td][td][/td][td][/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
6
[/td][td]
175007​
[/td][td]03[/td][td]
2019-07-23​
[/td][td]
2019-12-31​
[/td][td]Ignore[/td][td][/td][td]Ignore[/td][td][/td][td][/td][td][/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
7
[/td][td]
175007​
[/td][td]02[/td][td]
2018-01-01​
[/td][td]
2019-06-30​
[/td][td]Ignore[/td][td][/td][td]Ignore[/td][td][/td][td][/td][td][/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
8
[/td][td]
175007​
[/td][td]02[/td][td]
2018-01-01​
[/td][td]
2019-06-30​
[/td][td]Ignore[/td][td][/td][td]Ignore[/td][td][/td][td][/td][td][/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
9
[/td][td]
166109​
[/td][td]05[/td][td]
2019-03-11​
[/td][td]
2019-12-31​
[/td][td]Active[/td][td][/td][td]Active[/td][td][/td][td][/td][td][/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
10
[/td][td]
166109​
[/td][td]04[/td][td]
2019-01-01​
[/td][td]
2019-03-10​
[/td][td]Not Active[/td][td][/td][td]Not active[/td][td][/td][td][/td][td][/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
11
[/td][td]
166109​
[/td][td]03[/td][td]
2018-09-10​
[/td][td]
2018-12-31​
[/td][td]Not Active[/td][td][/td][td]Not active[/td][td][/td][td][/td][td][/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
12
[/td][td]
156108
[/td][td]02[/td][td]
2019-08-01
[/td][td][/td][td]Active[/td][td][/td][td]Ignore[/td][td][/td][td][/td][td][/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
13
[/td][td]
156108
[/td][td]01[/td][td]
2018-12-01
[/td][td]
2019-10-31
[/td][td]Active[/td][td][/td][td]Ignore[/td][td][/td][td][/td][td][/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
14
[/td][td]
256118
[/td][td]04[/td][td]
2019-05-06
[/td][td]
2019-08-30
[/td][td]Not Active[/td][td][/td][td]Ignore[/td][td][/td][td][/td][td][/td][/tr]


[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
15
[/td][td]
256118
[/td][td]03[/td][td]
2018-08-20
[/td][td]
2019-05-05
[/td][td]Not Active[/td][td][/td][td]Ignore[/td][td][/td][td][/td][td][/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Sheet: Sheet1[/td][/tr][/table]
 
Upvote 0
I've added two new use case, line 12-15. The script generates Active or Not active on both, but expected is Ignore on all four.
You have given no logic as to why they should be Ignore.

Row 12
In post 6 you said "EndDate can be blank were expected result should be Active."

Row 13
Post 1 "If employment (column B) differs then check if startdate (column C) and enddate (column D) is within range of "orange date" (B17) then Active"

Rows 14 & 15
Post 1 "If employment (column B) differs then check if startdate (column C) and enddate (column D) is within range of "orange date" (B17) then ... else Not active."

The code results seem to agree with all of those rules.

Further, the condition for Ignore was "If same employment (column B) occurs two times = all rows should be Ignore.". None of rows 12:15 meet that condition.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,620
Latest member
dsubash

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