Finding all possible combinations of multiple columns separated by delimiter

Hi,

I have a data set as the following
ID                                                    ID2                                                           String                                 String2 
1234                       33423,43222,442224,213432          Sample; repeat; example; multiple  second; possible; delimiter
2345                    12354; 55633; 343534; 65443;121121        data;set; sample; find               answer; combination; by

Open in new window




Now I want to find a Cartesian product of the following data set resulting in a data set with the same column headers but every possible combination of 1-4 columns from the next line of the headers

I have the following code which I was using to resolve the above problem. However, I could only perform Cartesian product for one row at a time. I am a newbie in vba and I tried all possible ways which I can think of to put the below code in a for loop but wasn't successful. Can anyone help me resolve this?


Sub Cartesian()
Dim MyStr1 As Variant, MyStr2 As Variant, MyStr3 As Variant, MyStr4 As Variant, Str1 As Variant, Str2 As Variant, Str3 As Variant, Str4 As Variant, X As Long
MyStr1 = Split(Range("A2").Text, ";")
MyStr2 = Split(Range("B2").Text, ";")
MyStr3 = Split(Range("C2").Text, ";")
MyStr4 = Split(Range("D2").Text, ";")
X = 2
For Each Str1 In MyStr1
    For Each Str2 In MyStr2
        For Each Str3 In MyStr3
            For Each Str4 In MyStr4
                Worksheets("Sheet6").Range("A" & X).Formula = Str1
                Worksheets("Sheet6").Range("B" & X).Formula = Str2
                Worksheets("Sheet6"). Range("C" & X).Formula = Str3
                Worksheets("Sheet6").Range("D" & X).Formula = Str4
                X = X + 1
            Next
        Next
    Next
Next
End Sub

Open in new window


Or is there a way I could achieve this using SQL within excel?
masuka 9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saqib Husain, SyedEngineerCommented:
Sub Cartesian()
Dim MyStr1 As Variant, MyStr2 As Variant, MyStr3 As Variant, MyStr4 As Variant, Str1 As Variant, Str2 As Variant, Str3 As Variant, Str4 As Variant, X As Long
    X = 2
For Each strset In Range("A2:A" & Range("A2").End(xlDown).Row)
    MyStr1 = Split(Range("A" & strset.Row).Text, ";")
    MyStr2 = Split(Range("B" & strset.Row).Text, ";")
    MyStr3 = Split(Range("C" & strset.Row).Text, ";")
    MyStr4 = Split(Range("D" & strset.Row).Text, ";")
    For Each Str1 In MyStr1
        For Each Str2 In MyStr2
            For Each Str3 In MyStr3
                For Each Str4 In MyStr4
                    Worksheets("Sheet2").Range("A" & X).Formula = Str1
                    Worksheets("Sheet2").Range("B" & X).Formula = Str2
                    Worksheets("Sheet2").Range("C" & X).Formula = Str3
                    Worksheets("Sheet2").Range("D" & X).Formula = Str4
                    X = X + 1
                Next
            Next
        Next
    Next
Next
End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Arno KosterCommented:
try this:

Sub Cartesian()
Dim MyStr1 As Variant, MyStr2 As Variant, MyStr3 As Variant, MyStr4 As Variant, Str1 As Variant, Str2 As Variant, Str3 As Variant, Str4 As Variant, X As Long
Dim row As Range

    X = 2

'-- loop through all used rows
For Each row In UsedRange.Rows

    '-- skip first row being the header
    If row.row > 1 Then
    
        '-- original code
        MyStr1 = Split(row.Cells(1).Text, ";")
        MyStr2 = Split(row.Cells(2).Text, ";")
        MyStr3 = Split(row.Cells(3).Text, ";")
        MyStr4 = Split(row.Cells(4).Text, ";")
    
        For Each Str1 In MyStr1
            For Each Str2 In MyStr2
                For Each Str3 In MyStr3
                    For Each Str4 In MyStr4
                        Worksheets("Sheet6").Range("A" & X).Formula = Str1
                        Worksheets("Sheet6").Range("B" & X).Formula = Str2
                        Worksheets("Sheet6").Range("C" & X).Formula = Str3
                        Worksheets("Sheet6").Range("D" & X).Formula = Str4
                        X = X + 1
                    Next
                Next
            Next
        Next
    End If
Next row

End Sub

Open in new window

Martin LissOlder than dirtCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.