List all possible combinations of a single range in two seperate columns

jemrisin

New Member
Joined
Apr 25, 2013
Messages
8
I have a single rangle of numbers in Column A, and in Columns B and C and want to list all possible combinations of these numbers. If there is a possible solution without using a Macro I would perfer that, but if not I'll take what I can get.

Example Desired Output:

Column A:
Column B:
Column C
0001
0001
0001
0002
0001
0002
0003
0001
0003
0002
0001
0002
0002
0002
0003
0003
0001
0003
0002
0003
0003

<TBODY>
</TBODY>
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
1-2 is the same as 2-1 if you are talking combinations. did you mean permutations?


Yes, I meant permutations. In this example 1-2 is NOT the same as 2-1, these would need to be listed seperately (see example). Sorry for the confusion, I was not aware of the difference in terminology.
 
Upvote 0
in B1 put =A1 in C1 put = A1 ( gives 0001 0001 )

in B2 put =A1 in C2 put = A2 ( gives 0001 0002 )

put all permutations in but look out you will get 0001 0001 twice ditto 0002 0003

you will need a macro to remove these so why not have a macro to do it right in the first place?
 
Upvote 0
Code:
Option Explicit
Sub Perms()
Dim lListLength As Long
Dim lCount As Long
Dim x As Long, y As Long
Dim wks As Worksheet
Dim cel As Range
Set wks = Sheets("Sheet1")
Set cel = wks.Range("A1")
lListLength = wks.Range("A" & wks.Rows.Count).End(xlUp).Row - 1
For x = 0 To lListLength
    For y = 0 To lListLength
    
    cel.Offset(lCount, 1) = cel.Offset(x, 0)
    cel.Offset(lCount, 2) = cel.Offset(y, 0)
    
    lCount = lCount + 1
    
    Next y
Next x
End Sub
 
Upvote 0
Code:
Option Explicit
Sub Perms()
Dim lListLength As Long
Dim lCount As Long
Dim x As Long, y As Long
Dim wks As Worksheet
Dim cel As Range
Set wks = Sheets("Sheet1")
Set cel = wks.Range("A1")
lListLength = wks.Range("A" & wks.Rows.Count).End(xlUp).Row - 1
For x = 0 To lListLength
    For y = 0 To lListLength
    
    cel.Offset(lCount, 1) = cel.Offset(x, 0)
    cel.Offset(lCount, 2) = cel.Offset(y, 0)
    
    lCount = lCount + 1
    
    Next y
Next x
End Sub

Unless I misunderstood, the first option will not work because there are 376 numbers in the list I am using meaning there will should be 141,376 rows of permutations created. I tried the macro above and recieved the error "Subscript out of range." I'm guessing this is a user error considering I have limited experience with Macros. Can you let me know what I did wrong:

-Ensured my data is in rows A1:A376
-hit Alt+F11
-Selected the appropriate sheet
-Pasted the code
-Hit the "Run" button
 
Upvote 0
Try:

Code:
Option Explicit
Sub Perms()
Dim lListLength As Long
Dim lCount As Long
Dim x As Long, y As Long
Dim wks As Worksheet
Dim cel As Range
Set wks = Activesheet
Set cel = wks.Range("A1")
lListLength = wks.Range("A" & wks.Rows.Count).End(xlUp).Row - 1
For x = 0 To lListLength
    For y = 0 To lListLength
    
    cel.Offset(lCount, 1) = cel.Offset(x, 0)
    cel.Offset(lCount, 2) = cel.Offset(y, 0)
    
    lCount = lCount + 1
    
    Next y
Next x
End Sub
 
Upvote 0
Try:

Code:
Option Explicit
Sub Perms()
Dim lListLength As Long
Dim lCount As Long
Dim x As Long, y As Long
Dim wks As Worksheet
Dim cel As Range
Set wks = Activesheet
Set cel = wks.Range("A1")
lListLength = wks.Range("A" & wks.Rows.Count).End(xlUp).Row - 1
For x = 0 To lListLength
    For y = 0 To lListLength
    
    cel.Offset(lCount, 1) = cel.Offset(x, 0)
    cel.Offset(lCount, 2) = cel.Offset(y, 0)
    
    lCount = lCount + 1
    
    Next y
Next x
End Sub

Genius! THANK YOU! I really need to learn VBA...
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,030
Members
448,940
Latest member
mdusw

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