Return the first occurrence after criteria is met

Shummy

New Member
Joined
Aug 5, 2022
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hello.
Thanks for helping.
I've been cracking my brain for hours however I am still unable to figure out a way to write a formula. What I have initially done is identified the number of occurrences for "Approval Request Submitted" with this formula: =IF(D2="Approval Request Submitted",COUNTIF($D$2:D2,"Approval Request Submitted"),"") and tried to incorporate a formula to find the MIN Event Date whenever the next occurrence occurs however I have no idea how to incorporate this logic.
Requisition IDCandidate IDEvent DateEvent
11234516/02/2022Approval Request Submitted
11234517/02/2022Correspondence Sent
11234518/02/2022Correspondence Sent
11234519/02/2022Correspondence Sent
11234519/02/2022Approval Request Submitted
11234520/02/2022Correspondence Sent
11234521/02/2022Correspondence Sent
11234521/02/2022Approval Request Submitted
11234521/02/2022Correspondence Sent

What I am trying to achieve is for each unique combination of Requisition ID and Candidate ID, I want to return the first occurrence of each Correspondence Sent after an Approval Request Submitted has occurred.
Requisition IDCandidate IDEvent DateEvent
11234517/02/2022Correspondence Sent
11234520/02/2022Correspondence Sent
11234521/02/2022Correspondence Sent

Above is only 1 case. I assume I would need to also incorporate a MATCH/INDEX formula as I need to identify each unique Requisition ID and Candidate ID combination.

Appreciate everyone's help.

Thanks!

Regards,
Chris
 

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.
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
Appreciate that. Sorry missed it when I was creating my account.
 
Upvote 0
Thanks for updating. (y)
  1. Is there any possible text in the 'Event' column other than the two shown in your sample?
  2. Is it possible to have two 'Approval Request Submitted' lines with no 'Correspondence Sent' between them? If so, what sould happen with that circumstance?
  3. In your sample, the line immediately after each 'Approval Request Submitted' line is 'Correspondence Sent'. Is that always the case?
  4. Would a vba solution be acceptable if a formula is not possible, or is very complicated?
 
Upvote 0
Thanks for updating. (y)
  1. Is there any possible text in the 'Event' column other than the two shown in your sample?
  2. Is it possible to have two 'Approval Request Submitted' lines with no 'Correspondence Sent' between them? If so, what sould happen with that circumstance?
  3. In your sample, the line immediately after each 'Approval Request Submitted' line is 'Correspondence Sent'. Is that always the case?
  4. Would a vba solution be acceptable if a formula is not possible, or is very complicated?
Hey Peter,
Thanks for the reply.
1. There are other events which can occur however I what I'm ideally doing is calculating the duration between the 2 dates between Approval and Correspondence.
2. It could be possible, if it does I would only acknowledge the first Approval Requested Submitted.
3. Not always the case.
4. I'm open to VBA.
Thanks and do let me know if you have any additional questions!
 
Upvote 0
Thanks for those responses. Based on them, you could try this macro.

VBA Code:
Sub Approval_Correspondence()
  Dim d As Object
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
  
  Set d = CreateObject("Scripting.Dictionary")
  d.CompareMode = 1
  a = Range("A2", Range("D" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 4)
  For i = 1 To UBound(a)
    If a(i, 4) = "Approval Request Submitted" Then
      d(a(i, 1) & "|" & a(i, 2)) = 1
    ElseIf a(i, 4) = "Correspondence Sent" Then
      If d.Exists(a(i, 1) & "|" & a(i, 2)) Then
        k = k + 1
        b(k, 1) = a(i, 1): b(k, 2) = a(i, 2): b(k, 3) = a(i, 3): b(k, 4) = a(i, 4)
        d.Remove (a(i, 1) & "|" & a(i, 2))
      End If
    End If
  Next i
  Range("A" & Rows.Count).End(xlUp).Offset(3).Resize(k, 4).Value = b
End Sub

Here are my sample data (rows 1:16) and results (rows 19:23)

Shummy.xlsm
ABCD
1Requisition IDCandidate IDEvent DateEvent
211234516/02/2022Approval Request Submitted
311234517/02/2022Correspondence Sent
411234518/02/2022Correspondence Sent
511234519/02/2022Correspondence Sent
611234519/02/2022Approval Request Submitted
711234520/02/2022Correspondence Sent
811234521/02/2022Correspondence Sent
911234521/02/2022Approval Request Submitted
1011234521/02/2022Correspondence Sent
1113333316/02/2022Approval Request Submitted
1213333317/02/2022Other text
1313333318/02/2022Correspondence Sent
1422222216/02/2022Approval Request Submitted
1522222217/02/2022Approval Request Submitted
1622222218/02/2022Correspondence Sent
17
18
1911234517/02/2022Correspondence Sent
2011234520/02/2022Correspondence Sent
2111234521/02/2022Correspondence Sent
2213333318/02/2022Correspondence Sent
2322222218/02/2022Correspondence Sent
Sheet1
 
Upvote 0
Thanks for those responses. Based on them, you could try this macro.

VBA Code:
Sub Approval_Correspondence()
  Dim d As Object
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
 
  Set d = CreateObject("Scripting.Dictionary")
  d.CompareMode = 1
  a = Range("A2", Range("D" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 4)
  For i = 1 To UBound(a)
    If a(i, 4) = "Approval Request Submitted" Then
      d(a(i, 1) & "|" & a(i, 2)) = 1
    ElseIf a(i, 4) = "Correspondence Sent" Then
      If d.Exists(a(i, 1) & "|" & a(i, 2)) Then
        k = k + 1
        b(k, 1) = a(i, 1): b(k, 2) = a(i, 2): b(k, 3) = a(i, 3): b(k, 4) = a(i, 4)
        d.Remove (a(i, 1) & "|" & a(i, 2))
      End If
    End If
  Next i
  Range("A" & Rows.Count).End(xlUp).Offset(3).Resize(k, 4).Value = b
End Sub

Here are my sample data (rows 1:16) and results (rows 19:23)

Shummy.xlsm
ABCD
1Requisition IDCandidate IDEvent DateEvent
211234516/02/2022Approval Request Submitted
311234517/02/2022Correspondence Sent
411234518/02/2022Correspondence Sent
511234519/02/2022Correspondence Sent
611234519/02/2022Approval Request Submitted
711234520/02/2022Correspondence Sent
811234521/02/2022Correspondence Sent
911234521/02/2022Approval Request Submitted
1011234521/02/2022Correspondence Sent
1113333316/02/2022Approval Request Submitted
1213333317/02/2022Other text
1313333318/02/2022Correspondence Sent
1422222216/02/2022Approval Request Submitted
1522222217/02/2022Approval Request Submitted
1622222218/02/2022Correspondence Sent
17
18
1911234517/02/2022Correspondence Sent
2011234520/02/2022Correspondence Sent
2111234521/02/2022Correspondence Sent
2213333318/02/2022Correspondence Sent
2322222218/02/2022Correspondence Sent
Sheet1
Hi Peter,
Thanks so much. It works really well, do you mind if you break down the code and explain to me what's been written? Would like to learn!
Regards,
Chris
 
Upvote 0
Thanks so much. It works really well,
You're welcome.

do you mind if you break down the code and explain to me what's been written? Would like to learn!
VBA Code:
Sub Approval_Correspondence()
  Dim d As Object
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
  
  'Set up a dictionary to record the unique Requisition/Candidate combos
  Set d = CreateObject("Scripting.Dictionary")
  d.CompareMode = 1
  'Read all the data into an array for faster processing
  a = Range("A2", Range("D" & Rows.Count).End(xlUp)).Value
  'Set up an array plenty big enough to hold the results
  ReDim b(1 To UBound(a), 1 To 4)
 ' Work through the rows of the data
  For i = 1 To UBound(a)
    'If it is an Approval row then record the Req/Candidate combo.
    'If it already exists in the dictionaru a new key is NOT recorded.
    If a(i, 4) = "Approval Request Submitted" Then
      d(a(i, 1) & "|" & a(i, 2)) = 1
    'Else if it is a Corresp row then ..
    ElseIf a(i, 4) = "Correspondence Sent" Then
      '.. IF that row's Req/Candidate combo is already in the dictioary
      If d.Exists(a(i, 1) & "|" & a(i, 2)) Then
        'Go to the next row in the results array ..
        k = k + 1
        ' .. write all the values into that results array row ..
        b(k, 1) = a(i, 1): b(k, 2) = a(i, 2): b(k, 3) = a(i, 3): b(k, 4) = a(i, 4)
        ' .. and delted that Req/Candidate key from the dictionary so that it can be added again if the combo arises again
        d.Remove (a(i, 1) & "|" & a(i, 2))
      End If
    End If
  Next i
  'Write the results array back into the worksheet
  Range("A" & Rows.Count).End(xlUp).Offset(3).Resize(k, 4).Value = b
End Sub
 
Upvote 0
You're welcome.


VBA Code:
Sub Approval_Correspondence()
  Dim d As Object
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
 
  'Set up a dictionary to record the unique Requisition/Candidate combos
  Set d = CreateObject("Scripting.Dictionary")
  d.CompareMode = 1
  'Read all the data into an array for faster processing
  a = Range("A2", Range("D" & Rows.Count).End(xlUp)).Value
  'Set up an array plenty big enough to hold the results
  ReDim b(1 To UBound(a), 1 To 4)
 ' Work through the rows of the data
  For i = 1 To UBound(a)
    'If it is an Approval row then record the Req/Candidate combo.
    'If it already exists in the dictionaru a new key is NOT recorded.
    If a(i, 4) = "Approval Request Submitted" Then
      d(a(i, 1) & "|" & a(i, 2)) = 1
    'Else if it is a Corresp row then ..
    ElseIf a(i, 4) = "Correspondence Sent" Then
      '.. IF that row's Req/Candidate combo is already in the dictioary
      If d.Exists(a(i, 1) & "|" & a(i, 2)) Then
        'Go to the next row in the results array ..
        k = k + 1
        ' .. write all the values into that results array row ..
        b(k, 1) = a(i, 1): b(k, 2) = a(i, 2): b(k, 3) = a(i, 3): b(k, 4) = a(i, 4)
        ' .. and delted that Req/Candidate key from the dictionary so that it can be added again if the combo arises again
        d.Remove (a(i, 1) & "|" & a(i, 2))
      End If
    End If
  Next i
  'Write the results array back into the worksheet
  Range("A" & Rows.Count).End(xlUp).Offset(3).Resize(k, 4).Value = b
End Sub
Ok that's actually really amazing I did not know VBA could be done in this way. Looks like I still have a lot more to learn.

Sorry Peter it seems there was one other condition which I missed when I ran your code. It seems there may be cases in which Correspondence Sent could be done before the Approval Request Submitted is done. For this I would need to also grab the first date in which the correspondence is sent. Below is a sample table.

Requisition IDCandidate IDEvent DateEvent
11234516/07/2022Correspondence Sent
11234517/07/2022Correspondence Sent
11234518/07/2022Approval Request Submitted
11234519/07/2022Correspondence Sent
11234520/07/2022Correspondence Sent
11234521/07/2022Approval Request Submitted
11234522/07/2022Correspondence Sent

Apologies again that I missed this.

Thanks again for your help!

Regards,
Chris
 
Upvote 0
It seems there may be cases in which Correspondence Sent could be done before the Approval Request Submitted is done.
That seems to make this a very different question to what was originally asked. I am now unsure of exactly what the 'logic' is of what is required.
For example, if "Correspondence Sent" appears somewhere between two rows with "Approval Request Submitted" I can't see how one would decide which "Approval Request Submitted" it related to.
To see if something was possible I would want to see a substantial set of sample data that had all the different variations in it, the expected results and explanation in relation to those results.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,244
Members
452,622
Latest member
Laura_PinksBTHFT

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