+ Reply to Thread
Results 1 to 5 of 5

Create programmatically list of combinations for choices from data sets in excel vba

  1. #1
    somethinglikeant
    Guest

    Create programmatically list of combinations for choices from data sets in excel vba

    Hi all,

    I am wondering if anyone knows of a neat way to do this:-
    Say I have 3 data sets
    Data set 1 is {Red,Blue,Green}
    Data set 2 is {Car, Lorry}
    Data set 3 is {Small,Large}

    Is there a way of programmatically creating a list of combinations of
    selections from these groups.
    I know there are 3*2*2 = 12 combinations
    I would like to create a list such as

    Red Car Small
    Red Car Large
    Red Lorry Small
    Red Lorry Large
    Blue Car Small
    Blue Car Large
    Blue Lorry Small
    Blue Lorry Large
    Green Car Small
    Green Car Large
    Green Lorry Small
    Green Lorry Large

    I would like this list to always total the number of combinations
    dependant on the number of data sets and elements in this set. I want a
    pure procedure to handle x data sets each with however many elements.

    I have each data set under headings in a spreadsheet.

    I have got so far with the code and become unstuck.

    Any help appreciated,

    somethinglikeant


  2. #2
    Tom Ogilvy
    Guest

    RE: Create programmatically list of combinations for choices from data

    Assume each group will at least two members
    Sub gencombinations()
    Dim rng1 as Range, rng2 as Range, rng3 as Range
    Dim cell1 as Range, cell2 as Range, cell3 as Range
    set rng1 = Range(cells(2,1),cells(2,1).End(xldown))
    set rng2 = Range(cells(2,2),cells(2,2).End(xldown))
    set rng3 = Range(cells(2,3),cells(2,3).End(xldown))
    for each cell1 in rng1
    for each cell2 in rng2
    for each cell3 in rng3
    cells(rw,4) = cell1
    cells(rw,5) = cell2
    cells(rw,6) = cell3
    Next cell3
    Next cell2
    Next cell1
    End Sub

    --
    Regards,
    Tom Ogilvy



    "somethinglikeant" wrote:

    > Hi all,
    >
    > I am wondering if anyone knows of a neat way to do this:-
    > Say I have 3 data sets
    > Data set 1 is {Red,Blue,Green}
    > Data set 2 is {Car, Lorry}
    > Data set 3 is {Small,Large}
    >
    > Is there a way of programmatically creating a list of combinations of
    > selections from these groups.
    > I know there are 3*2*2 = 12 combinations
    > I would like to create a list such as
    >
    > Red Car Small
    > Red Car Large
    > Red Lorry Small
    > Red Lorry Large
    > Blue Car Small
    > Blue Car Large
    > Blue Lorry Small
    > Blue Lorry Large
    > Green Car Small
    > Green Car Large
    > Green Lorry Small
    > Green Lorry Large
    >
    > I would like this list to always total the number of combinations
    > dependant on the number of data sets and elements in this set. I want a
    > pure procedure to handle x data sets each with however many elements.
    >
    > I have each data set under headings in a spreadsheet.
    >
    > I have got so far with the code and become unstuck.
    >
    > Any help appreciated,
    >
    > somethinglikeant
    >
    >


  3. #3
    somethinglikeant
    Guest

    Re: Create programmatically list of combinations for choices from data

    Many Thanks for this Tom,

    Here is the completed code for the application I have created with your
    help:-

    Sub CombinationsGenerator()

    'declare variables
    Dim numfields As Integer, numitems() As Integer
    Dim cl1, cl2, cl3, cl4, cl5, cl6, cl7, cl8, cl9, cl10 As Range
    Dim a As Integer: a = 2 'initialise
    Dim q As Integer: q = 1 'initialise

    'update status bar and switch off screenupdating and calculation
    Application.StatusBar = "Creating Combinations"
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    'count number of fields
    numfields = Application.WorksheetFunction.CountA(Range("A1:J1"))

    'count number of items in each field
    ReDim numitems(1 To numfields)
    For i = 1 To numfields
    numitems(i) = Application.WorksheetFunction.CountA(Columns(i)) - 1
    Next i

    'define data ranges
    ReDim datarange(1 To numfields)
    For i = 1 To numfields
    If numitems(i) > 0 Then
    Range(Cells(2, i), Cells(2 + numitems(i) - 1, i)).Select
    Else
    Range(Cells(2, i), Cells(2, i)).Select
    End If
    datarange(i) = Selection.Address
    Next i

    'prepare to write to output sheet
    Sheets("Output").Columns("A:M").ClearContents
    Sheets("Output").Select: [A2].Select

    'copy header labels to other sheets
    Sheets("Input").Rows("1:1").Copy Sheets("Output").Rows("1:1")
    Sheets("Input").Rows("1:1").Copy Sheets("Invalid").Rows("1:1")

    'write combinations
    On Error Resume Next
    For Each cl1 In Sheets("Input").Range(datarange(1))
    For Each cl2 In Sheets("Input").Range(datarange(2))
    For Each cl3 In Sheets("Input").Range(datarange(3))
    For Each cl4 In Sheets("Input").Range(datarange(4))
    For Each cl5 In Sheets("Input").Range(datarange(5))
    For Each cl6 In Sheets("Input").Range(datarange(6))
    For Each cl7 In Sheets("Input").Range(datarange(7))
    For Each cl8 In Sheets("Input").Range(datarange(8))
    For Each cl9 In Sheets("Input").Range(datarange(9))
    For Each cl10 In Sheets("Input").Range(datarange(10))
    Cells(a, 1) = cl1
    Cells(a, 2) = cl2
    Cells(a, 3) = cl3
    Cells(a, 4) = cl4
    Cells(a, 5) = cl5
    Cells(a, 6) = cl6
    Cells(a, 7) = cl7
    Cells(a, 8) = cl8
    Cells(a, 9) = cl9
    Cells(a, 10) = cl10
    Application.StatusBar = "Creating Combinations :- " & a - 1
    a = a + 1
    Next cl10
    Next cl9
    Next cl8
    Next cl7
    Next cl6
    Next cl5
    Next cl4
    Next cl3
    Next cl2
    Next cl1
    Columns("A:J").AutoFit

    'calculate the number of records created and inform via pop-up
    For i = 1 To numfields
    If numitems(i) > 0 Then totalrecords = q * numitems(i)
    q = totalrecords
    Next i
    MsgBox totalrecords & " combinations were generated", vbInformation,
    "Combinations Generator"

    'add script and index formulas to output tab
    Application.StatusBar = "Inserting Script and Index functions."
    Sheets("Input").Range("K2:L2").Copy
    Sheets("Output").Select: [K2].Select
    Do Until IsEmpty(ActiveCell.Offset(0, -10))
    ActiveCell.Offset(1, 0).Select
    Loop
    Range("K2:L" & ActiveCell.Row - 1).PasteSpecial xlPasteFormulas
    [A2].Select

    'reset calculation and screenupdating
    Application.CutCopyMode = False: Application.StatusBar = "Calculating"
    MsgBox "Procedure completed", vbInformation, "Combinations Generator"
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic: Calculate
    Application.StatusBar = ""

    End Sub

    If you would like me to send you the workbook to review or have any
    further commnets please let me know.

    Many Thanks again

    Tom Ogilvy wrote:
    > Assume each group will at least two members
    > Sub gencombinations()
    > Dim rng1 as Range, rng2 as Range, rng3 as Range
    > Dim cell1 as Range, cell2 as Range, cell3 as Range
    > set rng1 = Range(cells(2,1),cells(2,1).End(xldown))
    > set rng2 = Range(cells(2,2),cells(2,2).End(xldown))
    > set rng3 = Range(cells(2,3),cells(2,3).End(xldown))
    > for each cell1 in rng1
    > for each cell2 in rng2
    > for each cell3 in rng3
    > cells(rw,4) = cell1
    > cells(rw,5) = cell2
    > cells(rw,6) = cell3
    > Next cell3
    > Next cell2
    > Next cell1
    > End Sub
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    >
    >
    > "somethinglikeant" wrote:
    >
    > > Hi all,
    > >
    > > I am wondering if anyone knows of a neat way to do this:-
    > > Say I have 3 data sets
    > > Data set 1 is {Red,Blue,Green}
    > > Data set 2 is {Car, Lorry}
    > > Data set 3 is {Small,Large}
    > >
    > > Is there a way of programmatically creating a list of combinations of
    > > selections from these groups.
    > > I know there are 3*2*2 = 12 combinations
    > > I would like to create a list such as
    > >
    > > Red Car Small
    > > Red Car Large
    > > Red Lorry Small
    > > Red Lorry Large
    > > Blue Car Small
    > > Blue Car Large
    > > Blue Lorry Small
    > > Blue Lorry Large
    > > Green Car Small
    > > Green Car Large
    > > Green Lorry Small
    > > Green Lorry Large
    > >
    > > I would like this list to always total the number of combinations
    > > dependant on the number of data sets and elements in this set. I want a
    > > pure procedure to handle x data sets each with however many elements.
    > >
    > > I have each data set under headings in a spreadsheet.
    > >
    > > I have got so far with the code and become unstuck.
    > >
    > > Any help appreciated,
    > >
    > > somethinglikeant
    > >
    > >



  4. #4
    Registered User
    Join Date
    08-09-2010
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: Create programmatically list of combinations for choices from data sets in excel

    Hi evryone
    my problem is same as above but with little modification.
    generate all combinations where selecting only each item from each group is not necessary.
    there would be an input for each category for the number of items to be taken from that category.
    we can select 2 from cat1 , 1 from cat2 and 1 from cat3
    eg. red blue car small
    blue green car small etc

  5. #5
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Create programmatically list of combinations for choices from data sets in excel

    shiv.charan that thread is 4 years old. please start a new one linking to this if required
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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