+ Reply to Thread
Results 1 to 14 of 14

Generate all possible combinations from 4 sets.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-06-2006
    Location
    Spain
    MS-Off Ver
    Excel 2010 32Bit
    Posts
    545

    Generate all possible combinations from 4 sets.

    Using Excel 2010
    Hello,

    I am sure this is bit tricky question.

    1st option I want to make all possible combinations from the 4 data sets, are in the column C, D, E and F…Result in the column I:L

    2nd option I want to generate only those combinations they pass min & max sums as per value given in the cells I1 & I2.

    Note: Why I want 2nd option in this example there are only 26880 sets…but (for example if data increase in 4 columns say for in C=55, D=70, E=35, F=30…so this will be 55*70*35*30 = 4042500 combinations…so far restring min and max would be handy.

    I have tried to as much as possible clearer in the workbook I need count in the row D4:AO4 but if any question I will try to answer.

    Please help.

    Regards,
    Moti

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,872

    Re: Generate all possible combinations from 4 sets.

    This ?

    Option Explicit
    
    Sub demo1()
    
    Dim i As Long, ii As Long, iii As Long, iiii As Long, n As Long, j As Long
    Dim a, b
    
    a = Range("C6:F29")
    
    ReDim b(1 To 30000, 1 To 5)
    
    For i = 1 To 24
        For ii = 1 To 16
            For iii = 1 To 10
                For iiii = 1 To 7
                    n = n + 1
                    b(n, 1) = a(i, 1): b(n, 2) = a(ii, 2): b(n, 3) = a(iii, 3): b(n, 4) = a(iiii, 4)
                    For j = 1 To 4
                        b(n, 5) = b(n, 5) + b(n, j)
                    Next j
                Next iiii
            Next iii
        Next ii
    Next i
    [I6].Resize(30000, 5).Clear
    [I6].Resize(n, 5) = b
    [I6].Resize(n, 5).Borders.Weight = 2
    Columns(9).Resize(, 5).HorizontalAlignment = xlCenter
    End Sub
    Sub demo2()
    
    Dim i As Long, ii As Long, iii As Long, iiii As Long, n As Long, j As Long, x As Long
    Dim a, b
    
    a = Range("C6:F29")
    
    ReDim b(1 To 30000, 1 To 5)
    
    For i = 1 To 24
        For ii = 1 To 16
            For iii = 1 To 10
                For iiii = 1 To 7
                    x = a(i, 1) + a(ii, 2) + a(iii, 3) + a(iiii, 4)
                    If x >= [I1] And x <= [I2] Then
                        n = n + 1
                        b(n, 1) = a(i, 1): b(n, 2) = a(ii, 2): b(n, 3) = a(iii, 3): b(n, 4) = a(iiii, 4)
                        b(n, 5) = x
                    End If
                Next iiii
            Next iii
        Next ii
    Next i
    [I6].Resize(30000, 5).Clear
    [I6].Resize(n, 5) = b
    [I6].Resize(n, 5).Borders.Weight = 2
    Columns(9).Resize(, 5).HorizontalAlignment = xlCenter
    End Sub
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  3. #3
    Forum Contributor
    Join Date
    08-06-2006
    Location
    Spain
    MS-Off Ver
    Excel 2010 32Bit
    Posts
    545

    Re: Generate all possible combinations from 4 sets.

    @JohnTopley I like the both macros superb! And the both options worked fine. Excellent solutions!

    I appreciate you for your help. Good luck.

    My Best regards,
    Moti

  4. #4
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,513

    Re: Generate all possible combinations from 4 sets.

    you can setting how many group ,nGroup to result SUM
    Dim arAll, aCom, dic, Ngroup As Long
    Sub test1()
    Dim a, b, c, d: Ngroup = 4
    With Application
        a = .Transpose(Range("C6:C" & Cells(Rows.Count, 3).End(3).Row).Value) :  b = .Transpose(Range("D6:D" & Cells(Rows.Count, 4).End(3).Row).Value)
        c = .Transpose(Range("E6:E" & Cells(Rows.Count, 5).End(3).Row).Value) :  d = .Transpose(Range("F6:F" & Cells(Rows.Count, 6).End(3).Row).Value)
    End With
    ReDim arAll(1 To 4)
    arAll(1) = a: arAll(2) = b: arAll(3) = c: arAll(4) = d
    ReDim aCom(1 To 4)
    Set dic = CreateObject("Scripting.dictionary")
    combo 1
    If dic.Count Then
      With [o6]
         .Resize(, 5).EntireColumn.Clear :  .Offset(-1).Resize(, Ngroup + 1).Value = Array("Data1", "Data2", "Data3", "Data4", "Sum")
         .Resize(dic.Count, UBound(aCom)).Value = Application.index(dic.items, 0, 0)
         .Offset(, UBound(aCom)).Resize(dic.Count).Formula = "=sum(rc[-4]:rc[-1])" :  .CurrentRegion.Borders.Weight = 2
      End With
    End If
    dic.RemoveAll: Set dic = Nothing
    End Sub
    
    Sub combo(Optional index As Long = 1)
     Dim i As Long
     If index = Ngroup + 1 Then
       If Not dic.exists(Join(aCom, "|")) Then
         If Application.Sum(aCom) >= [i1].Value And Application.Sum(aCom) <= [i2].Value Then dic(Join(aCom, "|")) = aCom
       End If
     Else
        For i = 1 To UBound(arAll(index))
         aCom(index) = arAll(index)(i)
         combo index + 1
        Next i
     End If
    End Sub
    Attached Files Attached Files
    Last edited by daboho; 05-30-2024 at 01:38 AM.
    "Presh Star Who has help you *For Add Reputation!! And mark case as Solve"

  5. #5
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,872

    Re: Generate all possible combinations from 4 sets.

    Glad to have helped and thank you for the rep

  6. #6
    Forum Contributor
    Join Date
    08-06-2006
    Location
    Spain
    MS-Off Ver
    Excel 2010 32Bit
    Posts
    545

    Re: Generate all possible combinations from 4 sets.

    @daboho, your code is extraordinary! It recognizes the length of each group automatically and generates all combination as per min-max input multiplying group’s correctly Brilliant idea! (This way one code covers both options!) This is solved 100%...

    I appreciate you for your help. Good luck.

    My Best regards,
    Moti

  7. #7
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,513

    Re: Generate all possible combinations from 4 sets.

    Yes if you have More data Example from C:H, and want get result sum with Group 4,5,6 etc not all to revision
    Only litte part code
    For gr = 4 to Ubound(arAll)
    combo 1,gr
    Next gr
    'and change in  sub module
    sub combo(optional index as long = 1,byRef group as long)
    'and change part code
    If index > group +1 then
    Last edited by daboho; 05-30-2024 at 08:48 AM.

  8. #8
    Forum Contributor
    Join Date
    08-06-2006
    Location
    Spain
    MS-Off Ver
    Excel 2010 32Bit
    Posts
    545

    Re: Generate all possible combinations from 4 sets.

    @daboho, thank you for the additional Note: post#7 how to extend columns.

    I am stuck trying the code post#4 as per following extended data rows…example attached it take long time and excel does not respond please can you check what I am doing wrong?

    My Best regards,
    Moti
    Attached Files Attached Files
    Last edited by Motilulla; 05-30-2024 at 09:52 AM.

  9. #9
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,872

    Re: Generate all possible combinations from 4 sets.

    [Using my macro] I tested your latest file which has 4.6 Million + ( 70*55*40*30) combinations ALL of which fall within MIN/MAX limits

    So YES, it takes some time to produce the results .....2 minutes 15 secs (faster than Daboho which I aborted) BUT which will not fit on 1M+ rows available!
    Last edited by JohnTopley; 05-30-2024 at 12:41 PM.

  10. #10
    Forum Contributor
    Join Date
    08-06-2006
    Location
    Spain
    MS-Off Ver
    Excel 2010 32Bit
    Posts
    545

    Re: Generate all possible combinations from 4 sets.

    @JohnTopley, I got it you are correct but there is any way after 1M rows can continue to next columns is it possible?

    My Best regards,
    Moti

  11. #11
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,872

    Re: Generate all possible combinations from 4 sets.

    Of course it can be done, BUT of what practical use is 4M + rows of data?

  12. #12
    Forum Contributor
    Join Date
    08-06-2006
    Location
    Spain
    MS-Off Ver
    Excel 2010 32Bit
    Posts
    545

    Re: Generate all possible combinations from 4 sets.

    Quote Originally Posted by JohnTopley View Post
    Of course it can be done, BUT of what practical use is 4M + rows of data?
    @JohnTopley, my honest answer is may be not after looking into it or may be useful I am not sure…May I request you if it can be done. Please can you create it for me.

    My Best regards,
    Moti

+ 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. Get Reduced sets out of 1X2 full predict Combinations
    By Motilulla in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-15-2023, 06:59 AM
  2. Returning combinations of two sets of data
    By dw_22801 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-13-2021, 11:38 AM
  3. [SOLVED] Generate Combinations Using VBA
    By NeedForExcel in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 06-06-2021, 01:10 AM
  4. Replies: 3
    Last Post: 06-28-2020, 04:09 AM
  5. vba to generate combinations
    By thoandros in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-21-2018, 09:20 AM
  6. Generate Combinations
    By subrato312 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-04-2017, 12:53 PM
  7. Generate combinations
    By cjt_choccy in forum Excel General
    Replies: 6
    Last Post: 11-20-2011, 02:51 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