+ Reply to Thread
Results 1 to 13 of 13

Generate every combination from three columns.

  1. #1
    Forum Contributor
    Join Date
    12-12-2017
    Location
    New Zealand
    MS-Off Ver
    Ms Office 2016
    Posts
    449

    Lightbulb Generate every combination from three columns.

    Hello all,

    I am wanting to get a list of every possible combination of the 3 x cols A, B, C.
    I know there will be thousands of them.

    In cols F, G, H I have started with numbers 1 and 2 only, this is only the start and just a sample of what I'm needing.
    It would be fine if the results went to sheet 2.

    The resulting combinations must be in three columns like cols F, G, and H.
    All combinations will come from the range B2:D26 .

    Thanks.
    Attached Files Attached Files
    I am grateful for all answers to my questions .
    Also i give a reputation even if not answered .

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: Generate every combination from three columns.

    15,625 combinations
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Contributor
    Join Date
    12-12-2017
    Location
    New Zealand
    MS-Off Ver
    Ms Office 2016
    Posts
    449

    Re: Generate every combination from three columns.

    Thanks for that Trevor.
    Just realized as per usual I didn't supply entire correct question to the forum.
    There will be blanks cells as well, but the data always starts from col A, meaning as example col A might contain number 12 and cols B and D are blank.
    Or Col A and B contain text or numbers and col C is blank. If col C has data, then there is always data in cols A and B. If col B has data, then there is always data in col A.

    Hope that makes sense.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: Generate every combination from three columns.

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    12-12-2017
    Location
    New Zealand
    MS-Off Ver
    Ms Office 2016
    Posts
    449

    Re: Generate every combination from three columns.

    Thanks again Trevor for your perfect result, I forgot to mention my post three in my 1st post as mentioned, but I have done a single col A and two cols(A&B) manually.
    All is good now. Thank you.

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: Generate every combination from three columns.

    You're welcome. Thanks for the rep.

  7. #7
    Forum Contributor
    Join Date
    12-12-2017
    Location
    New Zealand
    MS-Off Ver
    Ms Office 2016
    Posts
    449

    Re: Generate every combination from three columns.

    Hello Trevor, back again, I forgot to say that cols C and or B can be blank.

    So, if both cols B and C are blank then results can only be cells B2 to B26. I would like the macro to result these as well.
    Also, if col C was blank then there would be results for two cols, A and B. I would like to result these as well.

    Hope this makes sense.
    Thanks.

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: Generate every combination from three columns.

    I have to be honest and say that I don't really understand most of the comments and riders.

    From what I can remember, it is a fairly straightforward triple loop. Where are the blanks? If your sample does not represent the reality, please provide one that does.

    Ensure that you also include expected results.

  9. #9
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Generate every combination from three columns.

    Suppose that you want to create unique distinct combinations from A-B-C (or A-B, or A only).
    Give it a try

    PHP Code: 
    Option Explicit
    Sub TEST
    ()
    Dim lr&, k&, i1&, i2&, i3&, rngres(1 To 10000001 To 3)
    Dim dic As Objectst$
    Set dic CreateObject("Scripting.Dictionary")
    lr Range("A1").CurrentRegion.Rows.Count
    rng 
    Range("B2:D" lr).Value
    For i1 1 To UBound(rng)
        For 
    i2 1 To UBound(rng)
            For 
    i3 1 To UBound(rng)
                
    st rng(i11) & "|" rng(i22) & "|" rng(i33)
                If 
    Not dic.exists(stThen
                    dic
    .Add st""
                    
    1res(k1) = rng(i11)
                    If 
    rng(i22) <> "" Then res(k2) = rng(i22)
                    If 
    rng(i33) <> "" Then res(k3) = rng(i33)
                
    End If
            
    Next
        Next
    Next
    Sheets
    ("Sheet2").Activate
    Range
    ("A2:C1000000").ClearContents
    Range
    ("A2").Resize(k3).Value res
    Set dic 
    Nothing
    End Sub 
    Attached Files Attached Files
    Quang PT

  10. #10
    Forum Contributor
    Join Date
    12-12-2017
    Location
    New Zealand
    MS-Off Ver
    Ms Office 2016
    Posts
    449

    Re: Generate every combination from three columns.

    Thanks Quang,
    I have removed all of col C data in this sheet.
    So, what changes would I make to result all combinations now.
    Thank you.
    Attached Files Attached Files

  11. #11
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Generate every combination from three columns.

    Nothing change
    Just remove C, or B and C, then run the code
    Attached Files Attached Files

  12. #12
    Forum Contributor
    Join Date
    12-12-2017
    Location
    New Zealand
    MS-Off Ver
    Ms Office 2016
    Posts
    449

    Re: Generate every combination from three columns.

    Thats great, problem now solved. Thanks for your help, Quang.
    Apologies to Trevor for not explaining properly.

  13. #13
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: Generate every combination from three columns.

    No problem. Thanks for the rep

+ 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. Generate combination of numbers
    By KKR1975 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-25-2020, 11:10 PM
  2. [SOLVED] Generate Heads Tails Combination Result VBA
    By MoldyBread in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-26-2018, 10:37 AM
  3. Generate Head Tale Combination Result
    By MoldyBread in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-26-2018, 08:18 AM
  4. [SOLVED] Need formula to generate combination of five columns with three possible answers in each
    By louhilla in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 04-26-2018, 03:33 PM
  5. How to generate a list of pairs for all combination possible?
    By treue in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-05-2015, 09:23 AM
  6. [SOLVED] How to generate a list of pairs for all combination possible?
    By Laeticha in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-05-2015, 08:56 AM
  7. I need to generate a list of combination
    By ciprianb in forum Excel - New Users/Basics
    Replies: 10
    Last Post: 02-28-2011, 01:28 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