Generate combinations of column with Fixed and column with Variable Values

SidKol

New Member
Joined
Oct 14, 2015
Messages
47
Hi all,

I'm playing around with VBA to find different ways of combination generation.

I have been able to create a tool to generate all possible combinations of FIVE values from one column
Order is not important
No repetition allowed

Input:
AB
1
2
3Variable
4C
5D
6E
7F
8G
9H
10I

<tbody>
</tbody>


Output: (all combinations of five values, no repetition, order not important)
RESULTS
CDEFG
CDEFH
CDEFI
CDEGH
CDEGI
CDEHI
CDFGH
CDFGI
CDFHI
CDGHI
CEFGH
CEFGI
CEFHI
CEGHI
CFGHI
DEFGH
DEFGI
DEFHI
DEGHI
DFGHI
EFGHI

<tbody>
</tbody>

This is the code:
Code:
Sub combigenerator()


    Dim X As Long
    Dim a As Long, b As Long, c As Long, d As Long, e As Long
    Dim TotalValuesB As Integer


    'start from row 20 with results
    X = 20
    
    'count all variables in column B
    TotalValuesB = WorksheetFunction.CountIf(Range("B4:B15"), ("*?"))
    
    
    'clear previous result data
    With Sheets("Sheet1")
    .Rows(X & ":" & .Rows.Count).Delete
    End With
    


    'Loop combinations (no repetition, order not important)
    For a = 4 To (TotalValuesB + 3)
        For b = (a + 1) To (TotalValuesB + 3)
            For c = (b + 1) To (TotalValuesB + 3)
                For d = (c + 1) To (TotalValuesB + 3)
                    For e = (d + 1) To (TotalValuesB + 3)
       


          'Create a row with results
                        Cells(X, 1) = Cells(a, 2)
                        Cells(X, 2) = Cells(b, 2)
                        Cells(X, 3) = Cells(c, 2)
                        Cells(X, 4) = Cells(d, 2)
                        Cells(X, 5) = Cells(e, 2)
                        
        X = X + 1


        Next e
        Next d
        Next c
        Next b
        Next a
End Sub


Now I want to add a second column with FIXED Values for the input

Input:

AB
1
2
3FIXEDVariable
4CE
5DF
6G
7H
8I

<tbody>
</tbody>


Meaning that each combination should always start with ALL fixed values.
In this example: A and B
(But can also be for example E, F and I)

So desired output in this case is:

CDEFG
CDEFH
CDEFI
CDEGH
CDEGI
CDEHI
CDFGH
CDFGI
CDFHI
CDGHI

<tbody>
</tbody>



I have been playing around with nested looping, GoTo Labels, ElseIf etc, but I am receiving error after error whatever I try.
Can somebody point me in the direction of an efficient way to make this combination generator work for both fixed and variable values?

Any help greatly appreciated :)
 
Last edited:

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.
Here's my solution:


Book1
ABCDE
3FixedVariableCount
4CE5
5DF
6G
7H
8I
20CDEFG
21CDEFH
22CDEFI
23CDEGH
24CDEGI
25CDEHI
26CDFGH
27CDFGI
28CDFHI
29CDGHI
Sheet1


Code:
Public Sub GenerateCombinations()

Const StartRow = 20

Dim itemCount As Long
Dim fixedCount As Long
Dim variableCount As Long
Dim i As Long
Dim nextRow As Long
Dim ticker As Long

' Clear out previous results
If Cells(StartRow, "A").Value <> "" Then
    Range(Cells(StartRow, "A"), Cells(Cells(Rows.Count, "A").End(xlUp).Row, "A")).EntireRow.ClearContents
End If

' Get counts
itemCount = Range("C4").Value
fixedCount = Cells(StartRow, "A").End(xlUp).Row - 3
variableCount = Cells(StartRow, "B").End(xlUp).Row - 3

' Set up counters
ReDim pickers(itemCount - fixedCount) As Long
For i = 1 To itemCount - fixedCount
    pickers(i) = i
Next i

' Start of creating all combinations here
nextRow = StartRow
Do While True
    ' Fixed values
    For i = 1 To fixedCount
        Cells(nextRow, i).Value = Cells(i + 3, "A").Value
    Next i
    
    ' Variable values
    For i = 1 To itemCount - fixedCount
        Cells(nextRow, i + fixedCount).Value = Cells(pickers(i) + 3, "B").Value
    Next i
    
    ' Tick
    nextRow = nextRow + 1
    ticker = itemCount - fixedCount
    Do While True
        pickers(ticker) = pickers(ticker) + 1
        If pickers(ticker) > variableCount - (itemCount - fixedCount - ticker) Then
            ticker = ticker - 1
            If ticker = 0 Then Exit Do
        Else
            Exit Do
        End If
    Loop
    
    ' Finished?
    If ticker = 0 Then Exit Do
    
    ' Set up subsequent pickers
    If ticker < itemCount - fixedCount Then
        For i = ticker + 1 To itemCount - fixedCount
            pickers(i) = pickers(i - 1) + 1
        Next i
    End If
Loop

End Sub

WBD
 
Upvote 0

Forum statistics

Threads
1,215,432
Messages
6,124,859
Members
449,194
Latest member
HellScout

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