Help regarding IF statement based on text and consecutive dates

jmu2013

New Member
Joined
Jan 29, 2014
Messages
4
I have been working on a spreadsheet on excel 2010 tracking the number of items a student can acquire. One student can have multiple teachers (uID). Items can be tested once or multiple times in a day. From columns A to E lists the raw data while column G contains filtered data of unique items while column H (=COUNTIF($B$2:$B$27,$G2)) is the number of times the item was tested for.

Now here is the really hard part. In order to be considered Completed (Column I), there are two options:
1. If an item is tested and acquired ("Yes" result on column E) by the student three times and on three different but consecutive dates (ex. 1/26 - 1/27 - 1/28 or 1/26 - 1/29/ - 2/5) and the testing is done by at least 2 different uIDs then the item is listed as Completed (column I).
2. If an item is acquired 5 times in the same day and tested the same uID, then the item is listed as Completed.

On DateMastered (Column J), it will only indicate the day the item was acquired based on the two conditions above.

Here are the IF statements I've been working on: I2 (=IF($E2="Yes","Yes",(IF($B2=$G2,"Yes","No")))) and J2 (=IF($I2="Yes",$C2,""))[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[TD]F[/TD]
[TD]G[/TD]
[TD]H[/TD]
[TD]I[/TD]
[TD]J[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]ID[/TD]
[TD]Item[/TD]
[TD]Date[/TD]
[TD]uID[/TD]
[TD]Acquired[/TD]
[TD][/TD]
[TD]Item[/TD]
[TD]NumofInstances[/TD]
[TD]Completed[/TD]
[TD]DateMastered[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]1[/TD]
[TD]lightbulb[/TD]
[TD]1/26/2013[/TD]
[TD]u0001[/TD]
[TD]Yes[/TD]
[TD][/TD]
[TD]lightbulb[/TD]
[TD]6[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]2[/TD]
[TD]lightbulb[/TD]
[TD]1/27/2013[/TD]
[TD]u0001[/TD]
[TD]Yes[/TD]
[TD][/TD]
[TD]lamp[/TD]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]3[/TD]
[TD]lamp[/TD]
[TD]1/27/2013[/TD]
[TD]u0001[/TD]
[TD]Yes[/TD]
[TD][/TD]
[TD]ball[/TD]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]4[/TD]
[TD]ball[/TD]
[TD]1/27/2013[/TD]
[TD]u0001[/TD]
[TD]No[/TD]
[TD][/TD]
[TD]hockeypuck[/TD]
[TD]2[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]5[/TD]
[TD]hockeypuck[/TD]
[TD]1/27/2013[/TD]
[TD]u0001[/TD]
[TD]No[/TD]
[TD][/TD]
[TD]card[/TD]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]6[/TD]
[TD]card[/TD]
[TD]1/27/2013[/TD]
[TD]u0001[/TD]
[TD]Yes[/TD]
[TD][/TD]
[TD]soccerball[/TD]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]7[/TD]
[TD]soccerball[/TD]
[TD]1/28/2013[/TD]
[TD]u0002[/TD]
[TD]Yes[/TD]
[TD][/TD]
[TD]laptop[/TD]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD]8[/TD]
[TD]lightbulb[/TD]
[TD]1/29/2013[/TD]
[TD]u0002[/TD]
[TD]No[/TD]
[TD][/TD]
[TD]pc[/TD]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]10[/TD]
[TD]9[/TD]
[TD]lightbulb[/TD]
[TD]1/29/2013[/TD]
[TD]u0001[/TD]
[TD]Yes[/TD]
[TD][/TD]
[TD]pc1[/TD]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]11[/TD]
[TD]10[/TD]
[TD]lightbulb[/TD]
[TD]1/29/2013[/TD]
[TD]u0001[/TD]
[TD]Yes[/TD]
[TD][/TD]
[TD]mirror[/TD]
[TD]5[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]12[/TD]
[TD]11[/TD]
[TD]lightbulb[/TD]
[TD]1/29/2013[/TD]
[TD]u0001[/TD]
[TD]Yes[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]13[/TD]
[TD]12[/TD]
[TD]laptop[/TD]
[TD]1/29/2013[/TD]
[TD]u0001[/TD]
[TD]No[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]14[/TD]
[TD]13[/TD]
[TD]pc[/TD]
[TD]1/30/2013[/TD]
[TD]u0002[/TD]
[TD]Yes[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]15[/TD]
[TD]14[/TD]
[TD]pc1[/TD]
[TD]1/30/2013[/TD]
[TD]u0002[/TD]
[TD]Yes[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]16[/TD]
[TD]15[/TD]
[TD]lamp[/TD]
[TD]1/30/2013[/TD]
[TD]u0001[/TD]
[TD]Yes[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]17[/TD]
[TD]16[/TD]
[TD]lamp[/TD]
[TD]1/30/2013[/TD]
[TD]u0001[/TD]
[TD]Yes[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]18[/TD]
[TD]17[/TD]
[TD]ball[/TD]
[TD]1/31/2013[/TD]
[TD]u0001[/TD]
[TD]Yes[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]19[/TD]
[TD]18[/TD]
[TD]ball[/TD]
[TD]2/1/2013[/TD]
[TD]u0001[/TD]
[TD]Yes[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]20[/TD]
[TD]19[/TD]
[TD]card[/TD]
[TD]2/2/2013[/TD]
[TD]u0001[/TD]
[TD]Yes[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]21[/TD]
[TD]20[/TD]
[TD]card[/TD]
[TD]2/3/2013[/TD]
[TD]u0001[/TD]
[TD]Yes[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]22[/TD]
[TD]21[/TD]
[TD]hockeypuck[/TD]
[TD]2/3/2013[/TD]
[TD]u0002[/TD]
[TD]No[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]23[/TD]
[TD]22[/TD]
[TD]mirror[/TD]
[TD]2/4/2013[/TD]
[TD]u0002[/TD]
[TD]Yes[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]24[/TD]
[TD]23[/TD]
[TD]mirror[/TD]
[TD]2/4/2013[/TD]
[TD]u0002[/TD]
[TD]Yes[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]25[/TD]
[TD]24[/TD]
[TD]mirror[/TD]
[TD]2/4/2013[/TD]
[TD]u0002[/TD]
[TD]Yes[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]26[/TD]
[TD]25[/TD]
[TD]mirror[/TD]
[TD]2/4/2013[/TD]
[TD]u0002[/TD]
[TD]Yes[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]27[/TD]
[TD]26[/TD]
[TD]mirror[/TD]
[TD]2/4/2013[/TD]
[TD]u0002[/TD]
[TD]Yes[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
. The results that I'm getting are not correct but I think this is the right direction. I appreciate any help or advice.
 

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.
So what should the results look like? Mirror is completed but nothing else?

And what do you mean for the DateMastered? Is the the first date acquired or the last or average?
 
Upvote 0
Sorry, I haven't updated or responded to this thread. You are correct bruderbell, only mirror would be completed since it fits the criteria for the item being tested 5 times in one day by the same uID with all of the results being "Acquired (Yes)."

Date Mastered would be the last day the item was acquired when the item is listed as "Completed." For the mirror item, the DateMastered would be 2/4/2013.[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD][/TD]
[TD]A
[/TD]
[TD]B
[/TD]
[TD]C
[/TD]
[TD]D
[/TD]
[TD]E
[/TD]
[/TR]
[TR]
[TD]28
[/TD]
[TD]ball
[/TD]
[TD]2/5/2013
[/TD]
[TD]u0002
[/TD]
[TD]Yes
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]29
[/TD]
[TD]card
[/TD]
[TD]2/5/2013
[/TD]
[TD]u0002
[/TD]
[TD]Yes
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]30
[/TD]
[TD]hockeypuck
[/TD]
[TD]2/5/2013
[/TD]
[TD]u0001
[/TD]
[TD]Yes
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]31
[/TD]
[TD]lamp
[/TD]
[TD]2/6/2013
[/TD]
[TD]u0001
[/TD]
[TD]Yes
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]32
[/TD]
[TD]soccerball
[/TD]
[TD]2/6/2013
[/TD]
[TD]u0002
[/TD]
[TD]No
[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


Lets say there was more info available. With this information, only ball and card were Completed and both were Mastered on 2/5/2013. Though lamp was "Acquired" on three different dates because it was tested by the same uID, it doesn't count.
 
Last edited:
Upvote 0
Now here is the really hard part. In order to be considered Completed (Column I), there are two options:
1. If an item is tested and acquired ("Yes" result on column E) by the student three times and on three different but consecutive dates (ex. 1/26 - 1/27 - 1/28 or 1/26 - 1/29/ - 2/5) and the testing is done by at least 2 different uIDs then the item is listed as Completed (column I).
2. If an item is acquired 5 times in the same day and tested the same uID, then the item is listed as Completed.

Define consecutive...your first example is what I'd call consecutive dates, the second is not. Do you mean three consecutive testings, with no dates being the same?
 
Upvote 0
Define consecutive...your first example is what I'd call consecutive dates, the second is not. Do you mean three consecutive testings, with no dates being the same?

Yup, sorry I was unclear. For the first condition, the dates don't have to be consecutive. As long as its three different dates that go in order for a specific item. The item has to be tested by two different uIDs with the three results being "Acquired." This will produce the result "Completed." The last date it was successfully tested on will be the DateMastered date.
 
Upvote 0
Ok, this can be done with user defined functions. I wrote two (and enjoyed learning how to do it). There's probably other ways to do this that are more elegant, but this is working for me. Here is the code (you may want to google and read about UDFs to understand where the code goes):
Code:
Function completed(datatable As Range, item As Range)
n = datatable.Rows.Count
Dim ItemArray()
ReDim ItemArray(1 To n)
Dim DateArray()
ReDim DateArray(1 To n)
Dim uIDArray()
ReDim uIDArray(1 To n)
Dim AcqArray()
ReDim AcqArray(1 To n)

'This section populates the VBA arrays that this formula uses to solve the problem.
'Note that it only populates dates if the item from the table matches the item requested.
For i = 1 To n
AcqArray(i) = datatable.Offset(i - 1, 4).Resize(1, 1).Value
ItemArray(i) = ((datatable.Offset(i - 1, 1).Resize(1, 1).Value = item) And (AcqArray(i) = "Yes"))
If ItemArray(i) = True Then
    DateArray(i) = datatable.Offset(i - 1, 2).Resize(1, 1).Value
    Else
    DateArray(i) = 0
End If
uIDArray(i) = datatable.Offset(i - 1, 3).Resize(1, 1).Value
Next i

'1. If an item is tested and acquired ("Yes" result on column E) by the student three times and on three different but consecutive dates (ex. 1/26 - 1/27 - 1/28 or 1/26 - 1/29/ - 2/5) _
' and the testing is done by at least 2 different uIDs then the item is listed as Completed (column I).
'This section tests for the above condition.
'Specifically it checks to see if there are 3 consecutive dates and that those dates all have corresponding "acquired = YES"
'and that at least two of the dates have different uIDs
For i = 1 To n
    For j = 1 To n
        For k = 1 To n
            If i <> j And j <> k And DateArray(i) <> 0 And DateArray(j) <> 0 And DateArray(k) <> 0 _
                And DateArray(i) < DateArray(j) And DateArray(i) > DateArray(k) _
                And ((uIDArray(i) <> uIDArray(j)) Or (uIDArray(i) <> uIDArray(k)) Or _
                (uIDArray(j) <> uIDArray(k))) Then
            completed = True
            Exit Function
            End If
        Next k
    Next j
Next i

'If an item is acquired 5 times in the same day and tested the same uID, then the item is listed as Completed.
'x is a counter for occurrences on the same date
'For the counter to advance, dates must match and uID must be the same
x = 1
For i = 1 To n
    For j = 1 To n
        If i <> j And DateArray(i) = DateArray(j) And DateArray(i) <> 0 And DateArray(j) <> 0 And uIDArray(i) = uIDArray(j) Then
        x = x + 1
        DateArray(j) = 0
        End If
    Next j
Next i
'Now we set the output of the function in accordance with the counter.  If 5 or more, it is set to true.  Otherwise, false.
completed = (x >= 5)
End Function
Function datecompleted(datatable As Range, item As Range)
n = datatable.Rows.Count
Dim ItemArray()
ReDim ItemArray(1 To n)
Dim DateArray()
ReDim DateArray(1 To n)
Dim uIDArray()
ReDim uIDArray(1 To n)
Dim AcqArray()
ReDim AcqArray(1 To n)

'This section populates the VBA arrays that this formula uses to solve the problem.
'Note that it only populates dates if the item from the table matches the item requested.
For i = 1 To n
AcqArray(i) = datatable.Offset(i - 1, 4).Resize(1, 1).Value
ItemArray(i) = ((datatable.Offset(i - 1, 1).Resize(1, 1).Value = item) And (AcqArray(i) = "Yes"))
If ItemArray(i) = True Then
    DateArray(i) = datatable.Offset(i - 1, 2).Resize(1, 1).Value
    Else
    DateArray(i) = 0
End If
uIDArray(i) = datatable.Offset(i - 1, 3).Resize(1, 1).Value
Next i

'1. If an item is tested and acquired ("Yes" result on column E) by the student three times and on three different but consecutive dates (ex. 1/26 - 1/27 - 1/28 or 1/26 - 1/29/ - 2/5) _
' and the testing is done by at least 2 different uIDs then the item is listed as Completed (column I).
'This section tests for the above condition.
'Specifically it checks to see if there are 3 consecutive dates and that those dates all have corresponding "acquired = YES"
'and that at least two of the dates have different uIDs
'If all that passes, then the return value is the first, largest date satisfying the condition
For i = 1 To n
    For j = 1 To n
        For k = 1 To n
            If i <> j And j <> k And DateArray(i) <> 0 And DateArray(j) <> 0 And DateArray(k) <> 0 _
                And DateArray(i) < DateArray(j) And DateArray(i) > DateArray(k) _
                And ((uIDArray(i) <> uIDArray(j)) Or (uIDArray(i) <> uIDArray(k)) Or _
                (uIDArray(j) <> uIDArray(k))) Then
            datecompleted = DateArray(j)
            Exit Function
            End If
        Next k
    Next j
Next i

'If an item is acquired 5 times in the same day and tested the same uID, then the item is listed as Completed.
'x is a counter for occurrences on the same date
'For the counter to advance, dates must match and uID must be the same.  Each time the counter advances, it saves a tentative "answer" value
x = 1
For i = 1 To n
    For j = 1 To n
        If i <> j And DateArray(i) = DateArray(j) And DateArray(i) <> 0 And DateArray(j) <> 0 And uIDArray(i) = uIDArray(j) Then
        x = x + 1
        answer = DateArray(j)
        DateArray(j) = 0
        End If
    Next j
Next i
'Now we set the output of the function in accordance with the counter.  If 5 or more, it is set to the date.  Otherwise, blank.
If x >= 5 Then datecompleted = answer Else datecompleted = ""
End Function

The code will create two UDFs. In the cell where you want the return value, you would put =completed("source data","item name"). The second one is =datecompleted("source data","item name"). Give it a try and let me know.
 
Upvote 0
Thank you bruderbell! I'll familiarize myself with VBA first then I will apply the code that you shared with me. I'll update you with my progress on the weekend.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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