Make a new VBA faster

motilulla

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

Here is example with 65 rows in the range B6:O10500 I have a set of combinations and in the range U6:AH4500 I have a set of results. (I want to check all the result against all combinations)

For that I am using formula in cell Q6 below. And the below VBA give me the results in cell R6 Below.

The problem when there is combination with 10500 rows and results with 4500 rows it is taking too much time to populate the results.

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

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

MrExcel.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAH
1
2
3
4105004500
5CombiP1P2P3P4P5P6P7P8P9P10P11P12P13P14Results Checked With FormulaResults Checked With VBAResultP1P2P3P4P5P6P7P8P9P10P11P12P13P14
61112111111111111414111211111111111
721121111111111X13132112111111111XX
8311211111111112131331121111111X11X
94112111111111X1131341121111111X1X1
105112111111111XX14145112111111X111X
116112111111111X213136112111111X11X1
1271121111111X11113137112111111XX111
1381121111111X11X14148112111111XX1XX
1491121111111X1121313911211111121112
15101121111111X1X11414101121111112X1X2
16111121111111X1XX13131111211111X1111X
17121121111111X1X213131211211111X111X1
1813112111111X111113131311211111X1X111
1914112111111X111X14141411211111X1X1XX
2015112111111X111213131511211111XX1111
2116112111111X11X114141611211111XX11XX
2217112111111X11XX13131711211111XXX11X
2318112111111X11X213131811211111XXX1X1
2419112111111XX11114141911211111X211X2
2520112111111XX11X13132011211111X2X112
2621112111111XX11213132111211111211112
2722112111111XX1X11313221121111121X1X2
2823112111111XX1XX141423112111112X11X2
2924112111111XX1X2131324112111112XX112
30251121111112111113132511211111221111
31261121111112111X131326112111112211XX
322711211111121112141427
3328112111111211X1131328
3429112111111211XX131329
3530112111111211X2131330
36311121111112X111131331
37321121111112X11X131332
38331121111112X112131333
39341121111112X1X1131334
40351121111112X1XX131335
41361121111112X1X2141436
423711211111X11111131337
433811211111X1111X141438
443911211111X11112131339
454011211111X111X1141440
464111211111X111XX131341
474211211111X111X2131342
484311211111X1X111141443
494411211111X1X11X131344
504511211111X1X112131345
514611211111X1X1X1131346
524711211111X1X1XX141447
534811211111X1X1X2131348
544911211111XX1111141449
555011211111XX111X131350
565111211111XX1112131351
575211211111XX11X1131352
585311211111XX11XX141453
595411211111XX11X2131354
605511211111XXX111131355
615611211111XXX11X141456
625711211111XXX112131357
635811211111XXX1X1141458
645911211111XXX1XX131359
656011211111XXX1X2131360
Check Results
Cell Formulas
RangeFormula
Q6:Q65Q6=MAX(MMULT(--($U$6:$AH$20000=B6:O6),TRANSPOSE(COLUMN($U$6:$AH$6))^0))
Press CTRL+SHIFT+ENTER to enter array formulas.


VBA Code:
Sub Formula_InTo_VBA()
Dim a, b, c
Dim i As Long, j As Long, k As Long, n As Long

Range("R6:R20000").ClearContents

Application.ScreenUpdating = False

Lr = Cells(Rows.Count, "B").End(xlUp).Row
a = Range("B6:O" & Lr)
ReDim c(1 To Lr)
Lr = Cells(Rows.Count, "U").End(xlUp).Row
b = Range("U6:AH" & Lr)

For i = 1 To UBound(a, 1)
    xmax = 0
    For j = 1 To UBound(b, 1)
        n = 0
        For k = 1 To 14
            If a(i, k) = b(j, k) Then n = n + 1
        Next k
        xmax = Application.Max(xmax, n)
        Next j
  
    c(i) = xmax
Next i

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

Application.ScreenUpdating = True

End Sub

Regards,
Moti
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Replace Application.Max(xmax, n), e.g.
VBA Code:
            'xmax = Application.Max(xmax, n)
            If n > xmax Then
                xmax = n
            End If
 
Upvote 1
Replace Application.Max(xmax, n), e.g.
VBA Code:
            'xmax = Application.Max(xmax, n)
            If n > xmax Then
                xmax = n
            End If
Hello rlv01, your 3 line code has reduced significant time from previous 00:04:21Minutes, to 00:01:13 Minutes. Great efforts I appreciate your help.

I will wait if it could be reduced more from the 00:01:13 Minutes to less time.

Kind Regards,
Moti :)
 
Upvote 0
I think you have seen most of the speed improvement you are going to see with your code in it's current form.
You have written your code using 3 nested loops.

VBA Code:
    For i = 1 To UBound(a, 1)
        For j = 1 To UBound(b, 1)
            For k = 1 To 14
                
            Next k
        Next j
    Next i

That's better than 50 million iterations of the inner loop for a 2000 row worksheet. Any further improvement would need a fundamental re-write that abandons the nested loops in favor of some other approach to get what you want.
 
Upvote 1
I think you have seen most of the speed improvement you are going to see with your code in it's current form.
You have written your code using 3 nested loops.

VBA Code:
    For i = 1 To UBound(a, 1)
        For j = 1 To UBound(b, 1)
            For k = 1 To 14
               
            Next k
        Next j
    Next i

That's better than 50 million iterations of the inner loop for a 2000 row worksheet. Any further improvement would need a fundamental re-write that abandons the nested loops in favor of some other approach to get what you want.
Hello rlv01, thank you for your reply. I do not have much idea of coding and I will be grateful of you if you can please re-write the new VBA to get results in less time.

Kind Regards,
Moti :)
 
Upvote 0
Hello rlv01, thank you for your reply. I do not have much idea of coding and I will be grateful of you if you can please re-write the new VBA to get results in less time.

Kind Regards,
Moti :)

I'm sorry, but I am not able to take on this job of work for you.
 
Upvote 1
I'm sorry, but I am not able to take on this job of work for you.
rlv01, no problem, thank you for your reply. I am already gratefully to you for your help reducing significant time.

I will wait May someone could help to rewrite and make it faster.

Good luck!

Kind Regards,
Moti :)
 
Upvote 0
You should know better than this.
Please provide the link to all & any crossposts.
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

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