All possible combination chart

Patmustard

New Member
Joined
Aug 2, 2011
Messages
2
Hi,
I'm trying to create an excel chart that will display all possible combinations of four variables. For example, I have the following possible fields:
Alpha: A,B
Numeric: 1,2
Symbol: @&
Alphanumeric: X1,Y1

Rather than having to manually create all the possible strings of the above, as below:
A,1,@,X1
A,1,@,Y1
B,1,@,X1
etc.

I'm trying to create a macro that will create it automatically.

Is this possible ?
Thanks !
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi, welcome to the board.

Yes, this is possible and the best way to approach this is to first
split each field in columnA - D.
this means your data table will look like
A, 1, @, X1
B, 2, , Y1

Then you will need a code to loop through each columns and create the combinations and fill them in columnE.

You can do it with the format you have but the method I've provided is definitely easier on the computer.

Anyways, after you've done that you can use this code
Code:
Sub Test()
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Dim i&, j&, k&, l&
    For i = 1 To Range("A" & Rows.count).End(xlUp).Row
        For j = 1 To Range("B" & Rows.count).End(xlUp).Row
            For k = 1 To Range("C" & Rows.count).End(xlUp).Row
                For l = 1 To Range("D" & Rows.count).End(xlUp).Row
                    Range("E" & count).Value = Range("A" & i).Value & ", " & Range("B" & j).Value _
                                            & ", " & Range("C" & k).Value & ", " & Range("D" & l).Value
                Next l
            Next k
        Next j
    Next i
    Application.ScreenUpdating = True
    Application.EnableEvents = True
End Sub
 
Upvote 0
I'm not sure which you would prefer but I was almost done writing a version with variables stored in vba when kpark posted his code:
Code:
Sub PermuteSet()
    Dim i, alpha, numeric, symbol, anum As Integer
    Dim alpArr(1), symArr(1), anumArr(1) As String
    alpArr(0) = "A"
    alpArr(1) = "B"
    symArr(0) = "@"
    symArr(1) = "&"
    anumArr(0) = "X1"
    anumArr(1) = "Y1"
    i = 1
    
    For alpha = LBound(alpArr) To UBound(alpArr)
        For numeric = 1 To 2
            For symbol = LBound(symArr) To UBound(symArr)
                For anum = LBound(anumArr) To UBound(anumArr)
                    Cells(i, 1) = alpArr(alpha) & "," & numeric & "," & symArr(symbol) & "," & anumArr(anum)
                    i = i + 1
                Next anum
            Next symbol
        Next numeric
    Next alpha
End Sub
 
Last edited:
Upvote 0
Hi - many thanks for getting back so quickly. I can see what you're doing, but I get a 'run time error 1004': 'Method 'Range of object'_Global' failed
grrrr !
 
Upvote 0
Hi,
I'm guessing you've tried moonfish's code.

Try this then
Code:
Sub PermuteSet()
    Dim i%, alpha%, numeric%, symbol%, anum As Integer
    Dim alpArr(0 To 1) As String , symArr(0 To 1) As String, anumArr(0 to 1) As String
    alpArr(0) = "A"
    alpArr(1) = "B"
    symArr(0) = "@"
    symArr(1) = "&"
    anumArr(0) = "X1"
    anumArr(1) = "Y1"
    i = 1
    
    For alpha = LBound(alpArr) To UBound(alpArr)
        For numeric = 1 To 2
            For symbol = LBound(symArr) To UBound(symArr)
                For anum = LBound(anumArr) To UBound(anumArr)
                    Cells(i, 1) = alpArr(alpha) & "," & numeric & "," & symArr(symbol) & "," & anumArr(anum)
                    i = i + 1
                Next anum
            Next symbol
        Next numeric
    Next alpha
End Sub
 
Upvote 0
A formula solution might be: in A1 through A16 =DEC2BIN(ROW()-1)+1111 and in B1 through B16 =CHOOSE(LEFT(A1,1),"A","B")&CHOOSE(MID(A1,2,1),1,2)&CHOOSE(MID(A1,3,1),"@","&")&CHOOSE(RIGHT(A1,1),"X1","Y1")
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,848
Members
452,948
Latest member
UsmanAli786

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