+ Reply to Thread
Results 1 to 5 of 5

All possible combinations speed up code

Hybrid View

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    All possible combinations speed up code

    Hello everyone
    I have this code that calculates all the possible combinations for the sum of specific numbers
    Option Explicit
    
    Sub PossibleCombinations()
        Dim r       As Variant
        Dim v       As Double
        Dim i       As Long
        Dim a       As Long
        Dim b       As Long
        Dim c       As Long
        Dim d       As Long
        Dim e       As Long
        Dim f       As Long
        Const x     As Long = 20
    
        ReDim r(1 To 1048576, 1 To 13)
    
        Application.ScreenUpdating = False
            For a = 1 To x
                For b = 1 To x
                    For c = 1 To x
                        For d = 1 To x
                            For e = 1 To x
                                For f = 1 To x
                                    v = Application.WorksheetFunction.Sum(a * 49, b * 99, c * 149, d * 199, e * 224, f * 249)
                                    If v = 14221 Then
                                        i = i + 1
                                        r(i, 1) = a
                                        r(i, 2) = b
                                        r(i, 3) = c
                                        r(i, 4) = d
                                        r(i, 5) = e
                                        r(i, 6) = f
                                        r(i, 7) = a * 49
                                        r(i, 8) = b * 99
                                        r(i, 9) = c * 149
                                        r(i, 10) = d * 199
                                        r(i, 11) = e * 224
                                        r(i, 12) = f * 249
                                        r(i, 13) = Application.WorksheetFunction.Sum(r(i, 7), r(i, 8), r(i, 9), r(i, 10), r(i, 11), r(i, 12))
                                        If i >= Rows.Count Then GoTo Skipper
                                    End If
                                Next f
                            Next e
                        Next d
                    Next c
                Next b
            Next a
    Skipper:
            Range("A1").Resize(i, UBound(r, 2)).Value = r
        Application.ScreenUpdating = True
    
        MsgBox "Done...", 64
    End Sub
    It is working well but it took about 6 minutes (Is there a way to make it faster?)

    Thanks advanced for help
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: All possible combinations speed up code

    This seems a bit faster :
    Sub PossibleCombinations()
        Dim a, p As Long, u As Long
        Dim i1 As Long, i2 As Long, i3 As Long, i4 As Long, i5 As Long, i6 As Long
        Dim tot1 As Long, tot2 As Long, tot3 As Long, tot4 As Long, tot5 As Long, tot6 As Long
    
        Debug.Print "Start at : " & Format$(Now, "HH:MM:SS")
        Const Target As Long = 14221
        Const x As Long = 20
        u = Rows.Count
        ReDim a(1 To 1048576, 1 To 13)
    
        Application.ScreenUpdating = False
        For i1 = 1 To x
            tot1 = i1 * 49
            If tot1 >= Target Then GoTo L1
            For i2 = 1 To x
                tot2 = tot1 + i2 * 99
                If tot2 >= Target Then GoTo L2
                For i3 = 1 To x
                    tot3 = tot2 + i3 * 149
                    If tot3 >= Target Then GoTo L3
                    For i4 = 1 To x
                        tot4 = tot3 + i4 * 199
                        If tot4 >= Target Then GoTo L4
                        For i5 = 1 To x
                            tot5 = tot4 + i5 * 224
                            If tot5 >= Target Then GoTo L5
                            For i6 = 1 To x
                                tot6 = tot5 + i6 * 249
                                If tot6 > Target Then GoTo L6
                                If tot6 = Target Then
                                   p = p + 1
                                   If p > u Then GoTo L1
                                   a(p, 1) = i1
                                   a(p, 2) = i2
                                   a(p, 3) = i3
                                   a(p, 4) = i4
                                   a(p, 5) = i5
                                   a(p, 6) = i6
                                   a(p, 7) = i1 * 49
                                   a(p, 8) = i2 * 99
                                   a(p, 9) = i3 * 149
                                   a(p, 10) = i4 * 199
                                   a(p, 11) = i5 * 224
                                   a(p, 12) = i6 * 249
                                   a(p, 13) = tot6
                                End If
                            Next i6
    L6:                 Next i5
    L5:             Next i4
    L4:         Next i3
    L3:     Next i2
    L2: Next i1
    L1:
    
        Range("A1").Resize(p, UBound(a, 2)).Value = a
        Application.ScreenUpdating = True
        Debug.Print "Stop at : " & Format$(Now, "HH:MM:SS")
        MsgBox "Done...", 64
    End Sub
    Last edited by karedog; 07-06-2017 at 10:03 AM.
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  3. #3
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: All possible combinations speed up code

    That's really awesome and fascinating .. Thank you very very much Mr. Karedog for this masterpiece
    Best and kind regards

  4. #4
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: All possible combinations speed up code

    You are welcome, thanks for marking the thread as solved and rep.points.
    As a guidance, you must avoid calling WorksheetFunction inside excessive loop, it takes a very big overhead.

    Regards

  5. #5
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: All possible combinations speed up code

    Quote Originally Posted by karedog View Post
    You are welcome, thanks for marking the thread as solved and rep.points.
    As a guidance, you must avoid calling WorksheetFunction inside excessive loop, it takes a very big overhead.

    Regards
    Thanks a lot for your useful guidance.. You are my savior all the time

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Help to speed up code
    By sintek in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-15-2016, 08:47 AM
  2. Help to speed up code
    By Raffe in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 11-30-2016, 02:54 PM
  3. [SOLVED] To speed up the code
    By gan_xl in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-23-2016, 11:07 AM
  4. [SOLVED] VBA Delete Row Code - Need more efficient code to speed it up
    By matcapir in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-02-2015, 04:32 PM
  5. Speed up code
    By ap1980 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-12-2012, 08:20 AM
  6. How to speed up code?
    By jp001 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-17-2009, 09:00 AM
  7. [SOLVED] Ned to speed up my code
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-14-2006, 01:15 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1