Macro to Print All Possible Combinations of 3 fields

luke2442

New Member
Joined
Aug 23, 2011
Messages
4
Hi All,

I've been trying to put together (read plagiarise!) a macro that will give me all the possible combinations of 3 columns of fields, which are "size", "colour", and "piping". Giving the results comma separated so i can split them with text to columns. This is what I have so far:

Code:
Sub com() 
Dim size As Range 
Dim colour As Range 
Dim piping As Range 

Dim combination As Range 
Dim counter 
Set combination = Worksheets(1).Range("D:D") 
counter = 1 
With Worksheets(1) 
    For Each size In .Range("A1", .Range("A" & .Rows.Count).End(xlUp)) 
        For Each colour In .Range("B1", .Range("B" & .Rows.Count).End(xlUp)) 
            For Each piping In .Range("C1", .Range("C" & .Rows.Count).End(x1Up)) 
                combination(counter) = size.Value & " , " & colour.Value & "," & piping.Value 
                counter = counter + 1 
             
         Next size 
        Next colour 
      Next piping 

End With 
End Sub

I get a "Invalid next control reference variable" error when I run it though....

Any Thoughts? Thanks!
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Then maybe like this

Code:
Sub com()
Dim size As Range
Dim colour As Range
Dim piping As Range

Dim counter As Long

counter = 1
With Worksheets(1)
    For Each size In .Range("A1", .Range("A" & .Rows.Count).End(xlUp))
        For Each colour In .Range("B1", .Range("B" & .Rows.Count).End(xlUp))
            For Each piping In .Range("C1", .Range("C" & .Rows.Count).End(xlUp))
                Range("D" & counter).Value = size.Value & " , " & colour.Value & "," & piping.Value
                counter = counter + 1
         Next size
        Next colour
      Next piping
End With
End Sub
 
Upvote 0
Try

Code:
Sub com()
Dim size As Range
Dim colour As Range
Dim piping As Range

Dim counter As Long

counter = 1
With Worksheets(1)
    For Each size In .Range("A1", .Range("A" & .Rows.Count).End(xlUp))
        For Each colour In .Range("B1", .Range("B" & .Rows.Count).End(xlUp))
            For Each piping In .Range("C1", .Range("C" & .Rows.Count).End(xlUp))
                .Range("D" & counter).Value = size.Value & " , " & colour.Value & "," & piping.Value
                counter = counter + 1
         Next piping
        Next colour
      Next size
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,570
Messages
6,179,611
Members
452,931
Latest member
The Monk

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