VBA Loop Lookup of every nth (eg. every 3rd) match of an item

Oraekene

New Member
Joined
Sep 20, 2022
Messages
46
Office Version
  1. 2013
Platform
  1. Windows
Hi. Good day. Grateful for all the help i've received on this site so far. Here again cap in hand seeking help.

I would like a vb script to loop through a range of values and find every nth eg. Every 3rd occurence of that value and copy and paste the single row of values in the next empty row in another range

Eg. I have customer data of 50 purchases made by 4 customers eg. A B C & D. Each purchase is a row of values showing date of purchase, item of purchase, price etc. I'd like to find every 3rd purchase A made (so her 3rd, 6th, 9th, 12th etc) and copy the row of data (date, item etc) to another range, then repeat for B, then repeat for C and D.

Would be grateful for any help on this. Attached is the sample sheet Sample Sheet
 
Last edited:

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Its just a modification of the original request
I don't understand what is required this time. Can you spell out in detail how we would decide which rows from that sample sheet get transferred to columns J to O?
That is, exactly how did you manually decide which rows to bold in columns A:F?
 
Upvote 0
I don't understand what is required this time. Can you spell out in detail how we would decide which rows from that sample sheet get transferred to columns J to O?
That is, exactly how did you manually decide which rows to bold in columns A:F?
Thank you for the response and apologies for my late reply. Here is the explanation

There are basically 2 criteria: the Account Numbers (the first column with the 10xe001, 10xe002 type numbers) and the project names (the second column with the Jaguda, Nsobu, mandela names). The whole range is sorted by time from the earliest to the latest time

What i hope to achieve is for ever project (eg. Mandela), i'd like to find the third unique wallet (eg. 10xe003) linked to that project. So for example Mandela might have rows with Mandela-10xe001, Mandela-10xe001, Mandela-10xe002, Mandela-10xe001, Mandela-10xe002,Mandela-10xe003. The row that would be selected would be the Mandela-10xe003 because that would be the row with the third unique wallet - 10xe003 - for the project named Mandela.

I don't know if this helps, i hope it does
 
Upvote 0
Thanks for the clarification. At this point I don't see an obvious solution for your excel version.
 
Upvote 0
Wanted to ask if there would be possible solutions on later excel versions?
Unfortunately your description in post 13 about how to decide which rows to return only covered one row, not how to also get subsequent rows. The best guess that I can give, which does not not match your given expected results**, is ..

** From what I understand, row 21 should be in the results since that row contains the 3rd unique value for mandela since the previously recorded mandela row (row 6)

Oraekene kantakura.xlsx
ABCDEFGJKLMNO
110xe001 jagudam 12/03/2022 A 1 i10xe003 mandela 14/03/2022 C 2 i
210xe001 jagudam 12/03/2022 A 1 ii10xe005 jagudam 18/03/2022 E 1 i
310xe002 mandela 12/03/2022 B 2 iFALSE10xe005 mandela 18/03/2022 E 2 i
410xe002 nsobu 3/12/2022 B 3 iFALSE10xe001 nsobu 19/03/2022 A 3 i
510xe001 mandela 13/03/2022 A 2 iFALSE10xe004 deeper life 20/03/2022 D 5 i
610xe003 mandela 14/03/2022 C 2 i TRUE10xe005 jagudam 22/03/2022 E 1 ii
710xe004 mandela 14/03/2022 D 1 iFALSE10xe002 jagudam 24/03/2022 B 1 ii
810xe002 caricature 14/03/2022 B 4 iFALSE10xe004 caricature 25/03/2022 D 4 i
910xe002 nsobu 14/03/2022 B 3 iiFALSE
1010xe002 deeper life 16/03/2022 B 5 iFALSE
1110xe001 deeper life 17/03/2022 A 5 iFALSE
1210xe001 jagudam 17/03/2022 A 1 iiiFALSE
1310xe004 jagudam 18/03/2022 D 1 iiFALSE
1410xe004 jagudam 18/03/2022 D 1 iiiFALSE
1510xe005 jagudam 18/03/2022 E 1 iTRUE
1610xe002 jagudam 18/03/2022 B 1 iFALSE
1710xe003 mandela 18/03/2022 C 2 iiFALSE
1810xe003 nsobu 18/03/2022 C 3 iFALSE
1910xe003 mandela 18/03/2022 C 2 iiiFALSE
2010xe003 mandela 18/03/2022 C 2 ivFALSE
2110xe005 mandela 18/03/2022 E 2 iTRUE
2210xe003 caricature 19/03/2022 C 4 iFALSE
2310xe001 nsobu 19/03/2022 A 3 iTRUE
2410xe001 deeper life 19/03/2022 A 5 iiFALSE
2510xe004 deeper life 20/03/2022 D 5 iTRUE
2610xe004 jagudam 22/03/2022 D 1 iiiFALSE
2710xe005 jagudam 22/03/2022 E 1 iiTRUE
2810xe003 jagudam 22/03/2022 C 1 iFALSE
2910xe004 jagudam 23/03/2022 D 1 ivFALSE
3010xe002 jagudam 24/03/2022 B 1 iiTRUE
3110xe001 jagudam 24/03/2022 A 1 ivFALSE
3210xe001 mandela 25/03/2022 A 2 iiFALSE
3310xe001 nsobu 25/03/2022 A 3 iiFALSE
3410xe001 mandela 25/03/2022 A 2 iiiFALSE
3510xe002 mandela 25/03/2022 B 2 iFALSE
3610xe002 mandela 25/03/2022 B 2 iiFALSE
3710xe004 caricature 25/03/2022 D 4 iTRUE
3810xe005 nsobu 25/03/2022 E 3 iFALSE
3910xe005 deeper life 25/03/2022 E 5 iFALSE
4010xe005 deeper life 26/03/2022 E 5 iiFALSE
4110xe005 jagudam 27/03/2022 E 1 iiiFALSE
4210xe001 jagudam 27/03/2022 A 1 vFALSE
4310xe005 jagudam 28/03/2022 E 1 ivFALSE
Sheet1
Cell Formulas
RangeFormula
J1:O8J1=FILTER(A1:F43,G1:G43)
G3:G43G3=LET(fr,IFNA(XMATCH(B3&TRUE,B$1:B2&G$1:G2,,-1),0)+1,MOD(ROWS(UNIQUE(FILTER(INDEX(A$1:A3,fr):A3,INDEX(B$1:B3,fr):B3=B3))),3)=0)
Dynamic array formulas.
 
Upvote 0
Unfortunately your description in post 13 about how to decide which rows to return only covered one row, not how to also get subsequent rows. The best guess that I can give, which does not not match your given expected results**, is ..

** From what I understand, row 21 should be in the results since that row contains the 3rd unique value for mandela since the previously recorded mandela row (row 6)

Oraekene kantakura.xlsx
ABCDEFGJKLMNO
110xe001 jagudam 12/03/2022 A 1 i10xe003 mandela 14/03/2022 C 2 i
210xe001 jagudam 12/03/2022 A 1 ii10xe005 jagudam 18/03/2022 E 1 i
310xe002 mandela 12/03/2022 B 2 iFALSE10xe005 mandela 18/03/2022 E 2 i
410xe002 nsobu 3/12/2022 B 3 iFALSE10xe001 nsobu 19/03/2022 A 3 i
510xe001 mandela 13/03/2022 A 2 iFALSE10xe004 deeper life 20/03/2022 D 5 i
610xe003 mandela 14/03/2022 C 2 i TRUE10xe005 jagudam 22/03/2022 E 1 ii
710xe004 mandela 14/03/2022 D 1 iFALSE10xe002 jagudam 24/03/2022 B 1 ii
810xe002 caricature 14/03/2022 B 4 iFALSE10xe004 caricature 25/03/2022 D 4 i
910xe002 nsobu 14/03/2022 B 3 iiFALSE
1010xe002 deeper life 16/03/2022 B 5 iFALSE
1110xe001 deeper life 17/03/2022 A 5 iFALSE
1210xe001 jagudam 17/03/2022 A 1 iiiFALSE
1310xe004 jagudam 18/03/2022 D 1 iiFALSE
1410xe004 jagudam 18/03/2022 D 1 iiiFALSE
1510xe005 jagudam 18/03/2022 E 1 iTRUE
1610xe002 jagudam 18/03/2022 B 1 iFALSE
1710xe003 mandela 18/03/2022 C 2 iiFALSE
1810xe003 nsobu 18/03/2022 C 3 iFALSE
1910xe003 mandela 18/03/2022 C 2 iiiFALSE
2010xe003 mandela 18/03/2022 C 2 ivFALSE
2110xe005 mandela 18/03/2022 E 2 iTRUE
2210xe003 caricature 19/03/2022 C 4 iFALSE
2310xe001 nsobu 19/03/2022 A 3 iTRUE
2410xe001 deeper life 19/03/2022 A 5 iiFALSE
2510xe004 deeper life 20/03/2022 D 5 iTRUE
2610xe004 jagudam 22/03/2022 D 1 iiiFALSE
2710xe005 jagudam 22/03/2022 E 1 iiTRUE
2810xe003 jagudam 22/03/2022 C 1 iFALSE
2910xe004 jagudam 23/03/2022 D 1 ivFALSE
3010xe002 jagudam 24/03/2022 B 1 iiTRUE
3110xe001 jagudam 24/03/2022 A 1 ivFALSE
3210xe001 mandela 25/03/2022 A 2 iiFALSE
3310xe001 nsobu 25/03/2022 A 3 iiFALSE
3410xe001 mandela 25/03/2022 A 2 iiiFALSE
3510xe002 mandela 25/03/2022 B 2 iFALSE
3610xe002 mandela 25/03/2022 B 2 iiFALSE
3710xe004 caricature 25/03/2022 D 4 iTRUE
3810xe005 nsobu 25/03/2022 E 3 iFALSE
3910xe005 deeper life 25/03/2022 E 5 iFALSE
4010xe005 deeper life 26/03/2022 E 5 iiFALSE
4110xe005 jagudam 27/03/2022 E 1 iiiFALSE
4210xe001 jagudam 27/03/2022 A 1 vFALSE
4310xe005 jagudam 28/03/2022 E 1 ivFALSE
Sheet1
Cell Formulas
RangeFormula
J1:O8J1=FILTER(A1:F43,G1:G43)
G3:G43G3=LET(fr,IFNA(XMATCH(B3&TRUE,B$1:B2&G$1:G2,,-1),0)+1,MOD(ROWS(UNIQUE(FILTER(INDEX(A$1:A3,fr):A3,INDEX(B$1:B3,fr):B3=B3))),3)=0)
Dynamic array formulas.
Yes yes yes you are right! It was my error. For mandela Row 21 should be the next after row 6, you've very correct. I made an error on my part
 
Upvote 0
How do i use this in vba?
Try this with a copy of your workbook.

VBA Code:
Sub Every_Nth()
  Dim d As Object
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, uba2 As Long
  
  Const N As Long = 3 '<- Edit to suit. This is for every 3rd one
  
  Set d = CreateObject("Scripting.Dictionary")
  a = Range("A1", Range("F" & Rows.Count).End(xlUp)).Value
  uba2 = UBound(a, 2)
  ReDim b(1 To UBound(a), 1 To uba2)
  For i = 1 To UBound(a)
    If InStr(1, d(a(i, 2)) & "|", "|" & a(i, 1) & "|") = 0 Then
      If UBound(Split(d(a(i, 2)), "|")) = N - 1 Then
        k = k + 1
        For j = 1 To uba2
          b(k, j) = a(i, j)
        Next j
        d.Remove a(i, 2)
      Else
        d(a(i, 2)) = d(a(i, 2)) & "|" & a(i, 1)
      End If
    End If
  Next i
  If k > 0 Then
    With Range("J1").Resize(k, uba2)
      .Value = b
      .Columns.AutoFit
    End With
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,179
Members
453,021
Latest member
Justyna P

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