Need Faster VBA, For Lottery Checker

motilulla

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

Here is example with 95 rows in the range B6:F53135 I have a set of 5 numbers combinations and in the range K6:O1332 I have a sets of results. (I want to check all the result against all combinations)

For that I am have formula in cell H3 below which have converted into VBA. The below VBA give me the results in cell H6 to down.

The problem when there is combination with 53130 rows and results with 1332 rows it is taking too much time (approximately 00:02:30 Minutes) to populate the results in the column H.

Please I need a help is there any fast VBA version to solve the time issue.

Here is below the example sheet with formula and VBA which I am using are attached.

Excel Questions.xlsm
ABCDEFGHIJKLMNOP
1
2
35
4531305313053130531305313013321332133213321332
5Combin1n2n3n4n5Match ResultsResultn1n2n3n4n5
61123455112345
72123464212678
8312347431291011
94123484412121314
105123494512151617
1161234104612181920
1271234114712212223
13812341248136912
149123413491371013
15101234144101381114
161112341541113151821
171212341641213161922
181312341741313172023
19141234184141461014
2015123419415147915
21161234204161481216
221712342141714111317
231812342241814182224
241912342341914192125
25201234244201561115
26211234254211571217
272212356422158913
28231235742315101618
29241235842415141923
30251235942515202124
312612351042616131620
322712351142716171825
332812351242817111621
342912351342917142022
353012351443017232425
363112351543118101519
373212351643219141624
3833123517433110122025
3934123518434111121823
4035123519435113152225
41361235204362361015
4237123521437237914
43381235224382381217
443912352343923111316
454012352444023182225
464112352544123192124
474212367442246913
4843123684432471012
4944123694442481115
504512361044524141618
514612361134624171922
524712361244724202324
53481236133482561216
54491236143492571118
55501236154502581014
56511236163512591519
575212361735225131720
585312361835326111417
595412361935426192325
605512362035527131521
615612362135627162025
62571236223572891621
635812362335828131823
645912362435929121824
6560123625360210132224
666112378461210171821
676212379462211122021
6863123710463214152425
6964123711364346711
7065123712365348910
716612371346634121315
726712371446734141721
736812371536834162325
7469123716369356818
75701237173703571516
76711237183713591117
777212371937235101219
787312372037335131422
797412372137436131719
807512372237536142024
81761237233763781920
827712372437737122122
837812372537837171824
84791238937938132125
858012381038038152223
868112381148139131820
8782123812482310112022
8883123813383310141823
8984123814484311122425
90851238153854561723
9186123816386457821
92871238174874591214
938812381838845101124
948912381938945131619
959012382039045151820
96911238213914681924
979212382239246121821
989312382339346151622
999412382439447131423
1009512382539548132022
Sheet Lottery
Cell Formulas
RangeFormula
H3H3=MAX(MMULT(COUNTIF(B6:F6,K$6:O$53135),{1;1;1;1;1}))
K4:O4,B4:F4B4=COUNTA(B6:B53135)
A7:A100A7=A6+1
Press CTRL+SHIFT+ENTER to enter array formulas.


VBA Code:
Option Explicit
Sub VBA_MultiLotteryChecker()
    
    Range("H6:H53135").ClearContents
    
    Dim startTime As Double
    Dim MinutesElapsed As String
    startTime = Timer
    
Dim a, b, c
Dim i As Long, j As Long, k As Long, n As Long, Lr As Long, l As Long
Dim xmax As Long

Application.ScreenUpdating = False

Lr = Cells(Rows.Count, "B").End(xlUp).row
a = Range("B6:H" & Lr)
ReDim c(1 To Lr)
Lr = Cells(Rows.Count, "K").End(xlUp).row
b = Range("K2:O" & Lr)

For i = 1 To UBound(a, 1)
    xmax = 0
    For j = 1 To UBound(b, 1)
        n = 0
        For k = 1 To 5
            For l = 1 To 5
                If a(i, k) = b(j, l) Then
                    n = n + 1
                    Exit For
                End If
            Next l
        Next k
          
           If n > xmax Then
                xmax = n
            End If

        Next j
    
    c(i) = xmax
Next i

[H6].Resize(UBound(c, 1), 1) = Application.Transpose(c)

Application.ScreenUpdating = True

   MinutesElapsed = Format((Timer - startTime) / 86400, "hh:mm:ss")
   MsgBox "This code ran successfully in " & MinutesElapsed & " minutes", vbInformation

End Sub

Regards,
Moti
 
@Peter_SSs, the changes will only speed it up if there are a fair proportion of 5 and 4 matches.
You are right of course as long as my dictionary key was consistent I could have left the leading |. Also thanks for the reminder on the VBA mid, I keep forgetting it works differently to the Excel mid function.
 
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
On my machine with your sample workbook the time reduced from 2:53 using the code in the workbook to 0:16 using the code below.
That is a significant reduction however I am not sure that your sample workbook is indicative of actual data since the 'Results' section appears to be multiple copies of the same set of 148 results. Therefore I don't expect my code to be as much faster on real data, though there are a few minor speed enhancements in it. Anyway, you can give it a try.
Hello Peter_SSs, I am so happy to get result in 00:0014 Minutes.

Yes the 148 result are multiplied by 9 to reach 1332 for trail. But your VBA results have accurate matches. (y)

When I run only with 1 result keeping K6:O6 line (eliminating rest all) highlighting the below line in yellow and give an error “9” please can you take a look.

VBA Code:
If a(i, k) = b(j, x) Then

I appreciate your kind help.

Have a good day nice start of the week ahead and Good Luck.

Regards,
Moti :)
 
Upvote 0
In the sample data the numbers in the rows are sorted in ascending order, if this can be relied on then the below modification of Peter's code might take a little bit more off the time.
Hello Alex Blakenburg, I am so happy to get result in 00:0012 Minutes. (y)

When I run only with 1 result keeping K6:O6 line (eliminating rest all) highlighting the below line in yellow and give an error “9” please can you take a look.

Code:
If a(i, k) = b(j, x) Then

I appreciate your kind help.

Have a good day nice start of the week ahead and Good Luck.

Regards,
Moti :)
 
Upvote 0
When I run only with 1 result keeping K6:O6 line (eliminating rest all) highlighting the below line in yellow and give an error “9” please can you take a look.
I'm not sure why you would run it with only 1 result. Surely you wouldn't be making any decisions on the basis of 1 result? :unsure:

Still if you want to do that, try this slight change about 1/3 of the way down in my code or in Alex's modification.

Rich (BB code):
Sub VBA_MultiLotteryChecker_v3()
  Dim startTime As Double
  Dim MinutesElapsed As String
  startTime = Timer
      
  Dim d As Object
  Dim s As String
  Dim a, b, c
  Dim i As Long, j As Long, k As Long, n As Long, Lr As Long, x As Long
  Dim xmax As Long, startcol As Long
  
  Set d = CreateObject("Scripting.Dictionary")
  Application.ScreenUpdating = False
  Range("H6:H53135").ClearContents
  Lr = Cells(Rows.Count, "B").End(xlUp).Row
  a = Range("B6:F" & Lr)
  
  Lr = Cells(Rows.Count, "K").End(xlUp).Row + 1
  b = Range("K6:O" & Lr)

  For i = 1 To UBound(b)
    s = Join(Application.Index(b, i, 0), "|")
    If Not d.exists(s) Then d(s) = i
  Next i
  b = Application.Index(b, Application.Transpose(d.Items), Array(1, 2, 3, 4, 5))
  ReDim c(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a, 1)
    xmax = 0
    For j = 1 To UBound(b, 1)
      n = 0
      startcol = 1
      For k = 1 To 5
        For x = startcol To 5
          If a(i, k) = b(j, x) Then
            n = n + 1
            startcol = x + 1
            Exit For
          End If
        Next x
      Next k
      If n > xmax Then xmax = n
    Next j
    c(i, 1) = xmax
  Next i
  Range("H6").Resize(UBound(c), 1).Value = c
  Application.ScreenUpdating = True

  MinutesElapsed = Format((Timer - startTime) / 86400, "hh:mm:ss")
  MsgBox "This code ran successfully in " & MinutesElapsed & " minutes", vbInformation
End Sub
 
Upvote 1
Solution
I'm not sure why you would run it with only 1 result. Surely you wouldn't be making any decisions on the basis of 1 result? :unsure:

Still if you want to do that, try this slight change about 1/3 of the way down in my code or in Alex's modification.
Hello Peter_SSs, all is working as I wanted fast and impeccable. I am happy. (y)

I am very grateful to you for your kind help.

Good day and Good Luck.

Kind Regards,
Moti :)
 
Upvote 0

Forum statistics

Threads
1,215,098
Messages
6,123,082
Members
449,094
Latest member
mystic19

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