Loop set of 3, within 5 results

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,422
Office Version
  1. 2010
Using Excel 2010

Hello, I got lottery results in column D:H approximately 2000+ rows, and set of triples in column K:M, I need a VBA which can loop set of 3… through column D:H and generate the count result in column O as shown in small example below

Count Triples And Find Dealy MrExcel.xlsm
ABCDEFGHIJKLMNOP
1
2Results
3Need VBA
4Draw S.NResultsTo Count
5Draw S.Nn1n2n3n4n5TriplesJoin TriplesTriples
6113/02/2004162932364116293216 | 29 | 322
7220/02/200416132932501251 | 2 | 53
8327/02/2004010205313712261 | 2 | 260
9405/03/2004010204053914161 | 4 | 160
10512/03/2004152428444714371 | 4 | 370
11619/03/2004333637424515211 | 5 | 211
12726/03/200403041023432782 | 7 | 80
13802/04/20040412242736629486 | 29 | 480
14909/04/20040105211923722487 | 22 | 480
151016/04/20040102030405912259 | 12 | 250
161123/04/2004061021454915182215 | 18 | 222
171230/04/2004050616232724273224 | 27 | 320
181307/05/2004151621363816293216 | 29 | 322
191414/05/200401032132391251 | 2 | 53
201521/05/2004152937394912261 | 2 | 260
211628/05/2004061135414414161 | 4 | 160
221704/06/2004091334414214371 | 4 | 370
231811/06/2004151822104715211 | 5 | 211
241918/06/200402232840432782 | 7 | 80
252025/06/20040321303435629486 | 29 | 480
262102/07/20040423242834722487 | 22 | 480
272209/07/20041516181922912259 | 12 | 250
282316/07/2004242631385015182215 | 18 | 222
292423/07/2004071027313424273224 | 27 | 320
302530/07/2004091019375016293216 | 29 | 322
312606/08/200405152435441251 | 2 | 53
322713/08/2004202741435012261 | 2 | 260
332820/08/2004060910273514161 | 4 | 160
342927/08/2004011122284414371 | 4 | 370
35
36
Sheet1


Regards,
Moti
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Give this a try. I have assumed that the leading zeros in columns D:H (eg "01") are the result of the cell formatting and that the underlying number in the cell is 1.

VBA Code:
Sub Count_Triples()
  Dim RX As Object
  Dim a As Variant, b As Variant, c As Variant
  Dim i As Long, j As Long
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  a = Range("D6", Range("H" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a)
    a(i, 1) = ":" & Join(Application.Index(a, i, 0), "::") & ":"
  Next i
  b = Range("K6", Range("M" & Rows.Count).End(xlUp)).Value
  ReDim c(1 To UBound(b), 1 To 1)
  For i = 1 To UBound(b)
    RX.Pattern = ":" & b(i, 1) & ":|:" & b(i, 2) & ":|:" & b(i, 3) & ":"
    c(i, 1) = 0
    For j = 1 To UBound(a)
      If RX.Execute(a(j, 1)).Count = 3 Then c(i, 1) = c(i, 1) + 1
    Next j
  Next i
  Range("O6").Resize(UBound(c)).Value = c
End Sub
 
Upvote 1
Give this a try. I have assumed that the leading zeros in columns D:H (eg "01") are the result of the cell formatting and that the underlying number in the cell is 1.
Hello Peter_SSs, VBA results are spot-on. I appreciate your time and help. Good luck. (y)

I tried to modify code to count quads but no luck, need your help once again please can you take a look and modify the code so it could work with the following data. I have removed the cell formatting and it is set to general
Count Triples And Find Dealy MrExcel.xlsm
ABCDEFGHIJKLMNOP
1
2Results
3Need VBA
4Draw S.NResultsTo Count
5Draw S.Nn1n2n3n4n5CuadsQuads
6113/02/2004162932364112340
7220/02/2004713394750125310
8327/02/200414181931371226310
9405/03/200447333739141650
10512/03/200415242844471437440
11619/03/200433363742451321321
12726/03/200434102343278230
13802/04/200441224273662948270
14909/04/20041410192372248190
151016/04/200414152835409122540
161123/04/2004610214549151822450
171230/04/200456162327242732230
181307/05/20041516213638162932361
191414/05/200413213239125320
201521/05/200415293739491226390
211628/05/20046113541441416410
221704/06/200491334414251524351
231811/06/200427810471521100
241918/06/2004223284043278400
252025/06/200432130343562948340
262102/07/200442324283472248280
272209/07/20042512194491225190
282316/07/20042426313850151822380
292423/07/2004710273134242732310
302530/07/2004910193750162932370
312606/08/2004515243544125350
322713/08/200420274143501226430
332820/08/2004691027351410191
342927/08/20041112228441437280
35
36
Sheet2

Best Regards,
Moti :)
 
Upvote 0
I tried to modify code to count quads ..
This code should count any number of multiples by changing the 'Const' line near the start. Also change the "O6" near the end, if required, to be the top cell of where the results should go.

VBA Code:
Option Explicit

Sub Count_Multiples()
  Dim RX As Object
  Dim a As Variant, b As Variant, c As Variant
  Dim i As Long, j As Long
 
  Const Multiples As Long = 4 '<- 2 for doubles, 3 for triples etc
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  a = Range("D6", Range("H" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a)
    a(i, 1) = ":" & Join(Application.Index(a, i, 0), "::") & ":"
  Next i
  b = Range("K6", Range("K" & Rows.Count).End(xlUp)).Resize(, Multiples).Value
  ReDim c(1 To UBound(b), 1 To 1)
  For i = 1 To UBound(b)
    RX.Pattern = ":" & Join(Application.Index(b, i, 0), ":|:") & ":"
    c(i, 1) = 0
    For j = 1 To UBound(a)
      If RX.Execute(a(j, 1)).Count = Multiples Then c(i, 1) = c(i, 1) + 1
    Next j
  Next i
  Range("O6").Resize(UBound(c)).Value = c
End Sub
 
Last edited:
Upvote 0
This code should count any number of multiples by changing the 'Const' line near the start. Also change the "O6" near the end, if required, to be the top cell of where the results should go.
Hello Peter_SSs, thank you for modifying code to count any number of multiples by changing the 'Const'

Here are my test results….
1st Post#2 macro “Sub Count_Triples()” when I tried with my original data as per post#1 layout with 1700+ rows result in columns D:H and 19000+ triples in the column K:M it took 68 seconds and when I tried Post#4 macro “Sub Count_Multiples()” changing 'Const' 3 it took time to finish 129 second just double than post#2 macro.

2nd post#4 macro “Sub Count_Multiples()” when I tried with my original data as per post#3 layout with 1700+ rows result in column D:H and 65000+ quads in the column K:N it blocked my computer (I create new workbook and tried several times but each time I need to reset computer) and the post#2 code cannot tried because it does not count quads.

Please I would like would you try it does it work with your computer, or may be this issue is my side only. I am running window 10 Pro. RAM 16,0 GB. Processor ntel(R) Core(TM) i7-6700 CPU @ 3.40GHz 3.40 GHz

Best Regards,
Moti
 
Upvote 0
Please I would like would you try it does it work with your computer,
I don't have any suitable data to test it with. However, I'm not surprised that it takes a long time (or fails) with the amount of data you are telling us about.
For example ..
I tried with my original data as per post#3 layout with 1700+ rows result in column D:H and 65000+ quads in the column K:N
Here there are 260,000 numbers in K:N and 10,200 in D:H. That gives a possible number of comparisons of 2,652,000,000
There may be a way to shorten it but nothing is obvious to me to make it a lot faster.
 
Upvote 0
Give this a try. I have assumed that the leading zeros in columns D:H (eg "01") are the result of the cell formatting and that the underlying number in the cell is 1.

VBA Code:
Sub Count_Triples()
  Dim RX As Object
  Dim a As Variant, b As Variant, c As Variant
  Dim i As Long, j As Long
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  a = Range("D6", Range("H" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a)
    a(i, 1) = ":" & Join(Application.Index(a, i, 0), "::") & ":"
  Next i
  b = Range("K6", Range("M" & Rows.Count).End(xlUp)).Value
  ReDim c(1 To UBound(b), 1 To 1)
  For i = 1 To UBound(b)
    RX.Pattern = ":" & b(i, 1) & ":|:" & b(i, 2) & ":|:" & b(i, 3) & ":"
    c(i, 1) = 0
    For j = 1 To UBound(a)
      If RX.Execute(a(j, 1)).Count = 3 Then c(i, 1) = c(i, 1) + 1
    Next j
  Next i
  Range("O6").Resize(UBound(c)).Value = c
End Sub
Hello Peter_SSs, thank you for the response as the Post#2 macro “Sub Count_Triples()” work fine, I tried to modified to use with quads Post#3 as following
VBA Code:
Following line
b = Range("K6", Range("M" & Rows.Count).End(xlUp)).Value
To this one
b = Range("K6", Range("N" & Rows.Count).End(xlUp)).Value


And next line
If RX.Execute(a(j, 1)).Count = 3 Then c(i, 1) = c(i, 1) + 1
To this one
If RX.Execute(a(j, 1)).Count = 4 Then c(i, 1) = c(i, 1) + 1

But getting count result '0' I guess it needs more line to modify. Please help to modify above code to work with Quad, so I can try and I am sure will work better the code post#4.

Best Regards,
Moti
 
Upvote 0
It also requires this modification but I think you will find any improvement is pretty small.

Rich (BB code):
RX.Pattern = ":" & b(i, 1) & ":|:" & b(i, 2) & ":|:" & b(i, 3) & ":"
RX.Pattern = ":" & b(i, 1) & ":|:" & b(i, 2) & ":|:" & b(i, 3) & ":|:" & b(i, 4) & ":"
 
Upvote 1
Try this code:

VBA Code:
Sub Count_lottery()
Dim a As Variant, b As Variant, c As Variant
Dim i&, j&, ra&, rb&, temp$
Dim t As Double
t = Timer
a = Range("D6", Range("H" & Rows.Count).End(xlUp)).Value
b = Range("K6", Range("N" & Rows.Count).End(xlUp)).Value
ra = UBound(a, 1)
rb = UBound(b, 1)
ReDim c(1 To rb, 1 To 2)
For i = 1 To rb
    c(i, 2) = "*" & Format(b(i, 1), "00") & "|*" & Format(b(i, 2), "00") & "|*" & Format(b(i, 3), "00") & "|*" & Format(b(i, 4), "00") & "|*"
Next i
For j = 1 To ra
    temp = Format(a(j, 1), "00|") & Format(a(j, 2), "00|") & Format(a(j, 3), "00|") & Format(a(j, 4), "00|") & Format(a(j, 5), "00|")
    For i = 1 To rb
        If temp Like c(i, 2) Then c(i, 1) = c(i, 1) + 1
    Next i
Next j
Range("P6").Resize(rb, 1) = c
MsgBox Timer - t
End Sub
 
Upvote 1
Solution
It also requires this modification but I think you will find any improvement is pretty small.

Rich (BB code):
RX.Pattern = ":" & b(i, 1) & ":|:" & b(i, 2) & ":|:" & b(i, 3) & ":"
RX.Pattern = ":" & b(i, 1) & ":|:" & b(i, 2) & ":|:" & b(i, 3) & ":|:" & b(i, 4) & ":"
Hello Peter_SSs, Thank you, replacing above line with Post#4 macro I tried with my original data as per post#3 layout with 1700+ rows result in column D:H and 65000+ quads in the column K:N, it took 215 second to finish. I tried again Post#4 macro but with this excel do not respond. I appreciate your help. Have a pleasant time. Good Luck.

Best Regards,
Moti :)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,194
Members
452,616
Latest member
intern444

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