How to generate all possible combinations of two lists (without MACRO)?

erangai

New Member
Joined
Aug 22, 2012
Messages
6
Hi everyone,

I need to make a list of all possible combinations of two lists as shown in the following example.
i.e. Basically when I update Column A & B, the combinations should get appear in the “Output” column.
Also need to do this without running a MACRO.

Can someone kindly help me?

Example
Column A
Column B

Output (Combinations)
Mar
AA

MarAA
Apr
BB

MarBB
May
CC

MarCC

DD

MarDD



AprAA



AprBB



AprCC



AprDD



MayAA



MayBB



MayCC



MayDD
 
Last edited by a moderator:

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Awesome, that is almost exactly what I am looking for! I like that I can use any number of columns in sheet 1 and I can stop the output at row 1,000,000, but is it possible to have it to put the output in separate columns and then skip 4 columns and start with the next set of permutations?

To keep it simple, I only used 2 columns of input in my example:

Here is the input for sheet 1:

A
B
1
aa11
11
2
2
c
3
b
33

<tbody>
[TD="colspan: 3"]INPUT
[/TD]

</tbody>

Then I get this:

A
1
aa11
2
aac
3
aa33
4
211
5
2c
6
233
7
b11
8
bc
9
b33

<tbody>
[TD="colspan: 2"]CURRENT
[/TD]

</tbody>

How can I change the code to get this instead in sheet 2 where the permutations are broken out into individual cells and 4 columns are left in between each 1,000,000 rows of permutations like so:

A
B
C
D
E
F
G
H
I
J
1
aa
11




After Row 1,000,000 the Next permutations start here



2
aa
c








3
aa
33








4
2
11








5
2
c








6
2
33








7
b
11








8
b
c








9
B
33









<tbody>
[TD="colspan: 11"]DESIRED
[/TD]

</tbody>

Here is your code below that was used (also the words input, current, and desired are not needed, they were only used to help with this example):

Sub Permute()
Dim ix(100, 1) As Long, rc As Long, m As Long, br As Long, md As Variant, i As Long, r As Long
Dim str1 As String, r1 As Long, c1 As Long

rc = Cells(1, Columns.Count).End(xlToLeft).Column
m = 0
For i = 1 To rc
br = Cells(Rows.Count, i).End(xlUp).Row
If br > m Then m = br
ix(i, 0) = br
ix(i, 1) = 1
Next i
md = Range(Cells(1, 1), Cells(m, rc)).Value

r = 0
Incr:
str1 = ""
For i = 1 To rc
str1 = str1 & md(ix(i, 1), i)
Next i
r = r + 1
r1 = ((r - 1) Mod 1000000) + 1
c1 = Int((r - 1) / 1000000) + 1
Sheets("Sheet2").Cells(r1, c1) = str1

For i = rc To 1 Step -1
ix(i, 1) = ix(i, 1) + 1
If ix(i, 1) <= ix(i, 0) Then Exit For
ix(i, 1) = 1
Next i
If i > 0 Then GoTo Incr:

End Sub



 
Upvote 0
Try:

Rich (BB code):
Sub Permute()
Dim ix(100, 1) As Long, rc As Long, m As Long, md As Variant, i As Long, r As Long, c As Long
Dim MyOut() As Variant, mor As Long, cbr As Long

' Set your max output rows here:
    mor = 1000000
' Set columns between results here:
    cbr = 4
    
    rc = Cells(1, Columns.Count).End(xlToLeft).Column
    m = 0
    For i = 1 To rc
        ix(i, 0) = Cells(Rows.Count, i).End(xlUp).Row
        ix(i, 1) = 1
        m = IIf(ix(i, 0) > m, ix(i, 0), m)
    Next i
    md = Range(Cells(1, 1), Cells(m, rc)).Value
    ReDim MyOut(1 To mor, 1 To rc)

    r = 0
    c = 1
Incr:
    r = r + 1
    If r > mor Then
        Sheets("Sheet2").Cells(1, c).Resize(mor, rc).Value = MyOut
        r = 1
        c = c + rc + cbr
        ReDim MyOut(1 To mor, 1 To rc)
    End If
    
    For i = 1 To rc
        MyOut(r, i) = md(ix(i, 1), i)
    Next i
    
    For i = rc To 1 Step -1
        ix(i, 1) = ix(i, 1) + 1
        If ix(i, 1) <= ix(i, 0) Then GoTo Incr:
        ix(i, 1) = 1
    Next i
    
    Sheets("Sheet2").Cells(1, c).Resize(mor, rc).Value = MyOut
    
End Sub
Change the values in red as desired. I tweaked it a bit to make it a bit faster. But I reiterate, I don't see the purpose of printing out millions of combinations. Better to analyze them as created if need be.
 
Upvote 0
Eric, you are the genius that I have read you are!You are correct, it is too much data.

Is there a way where I enter the input below, and it does not print duplicates?
[TABLE="width: 0, align: left"]
<tbody>[TR]
[TD]1
[/TD]
[TD]1
[/TD]
[TD]1
[/TD]
[TD]1
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]2
[/TD]
[TD]2
[/TD]
[TD]2
[/TD]
[/TR]
[TR]
[TD]3
[/TD]
[TD]3
[/TD]
[TD]3
[/TD]
[TD]3
[/TD]
[/TR]
[TR]
[TD]4
[/TD]
[TD]4
[/TD]
[TD]4
[/TD]
[TD]4
[/TD]
[/TR]
[TR]
[TD]5
[/TD]
[TD]5
[/TD]
[TD]5
[/TD]
[TD]5
[/TD]
[/TR]
[TR]
[TD]6
[/TD]
[TD]6
[/TD]
[TD]6
[/TD]
[TD]6
[/TD]
[/TR]
</tbody>[/TABLE]








These are examples of permutations that would not show up in sheet 2:
[TABLE="width: 0"]
<tbody>[TR]
[TD]1
[/TD]
[TD]1
[/TD]
[TD]1
[/TD]
[TD]1
[/TD]
[/TR]
[TR]
[TD]1
[/TD]
[TD]2
[/TD]
[TD]2
[/TD]
[TD]1
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]2
[/TD]
[TD]3
[/TD]
[TD]3
[/TD]
[/TR]
[TR]
[TD]4
[/TD]
[TD]1
[/TD]
[TD]1
[/TD]
[TD]4
[/TD]
[/TR]
[TR]
[TD]5
[/TD]
[TD]3
[/TD]
[TD]5
[/TD]
[TD]1
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]6
[/TD]
[TD]4
[/TD]
[TD]6
[/TD]
[/TR]
</tbody>[/TABLE]

These are examples of permutations that would show up in sheet 2
[TABLE="width: 0"]
<tbody>[TR]
[TD]1
[/TD]
[TD]2
[/TD]
[TD]3
[/TD]
[TD]4
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]4
[/TD]
[TD]5
[/TD]
[TD]6
[/TD]
[/TR]
[TR]
[TD]1
[/TD]
[TD]2
[/TD]
[TD]5
[/TD]
[TD]6
[/TD]
[/TR]
[TR]
[TD]4
[/TD]
[TD]1
[/TD]
[TD]2
[/TD]
[TD]5
[/TD]
[/TR]
[TR]
[TD]5
[/TD]
[TD]3
[/TD]
[TD]5
[/TD]
[TD]1
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]5
[/TD]
[TD]4
[/TD]
[TD]6
[/TD]
[/TR]
</tbody>[/TABLE]

Also is there a way to have only one of the following show up in sheet 2 since they are all virtually the same 4 numbers:

[TABLE="width: 0"]
<tbody>[TR]
[TD]1
[/TD]
[TD]2
[/TD]
[TD]3
[/TD]
[TD]4
[/TD]
[/TR]
[TR]
[TD]1
[/TD]
[TD]2
[/TD]
[TD]4
[/TD]
[TD]3
[/TD]
[/TR]
[TR]
[TD]1
[/TD]
[TD]3
[/TD]
[TD]2
[/TD]
[TD]4
[/TD]
[/TR]
[TR]
[TD]1
[/TD]
[TD]3
[/TD]
[TD]4
[/TD]
[TD]2
[/TD]
[/TR]
[TR]
[TD]1
[/TD]
[TD]4
[/TD]
[TD]3
[/TD]
[TD]2
[/TD]
[/TR]
[TR]
[TD]1
[/TD]
[TD]4
[/TD]
[TD]2
[/TD]
[TD]3
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]1
[/TD]
[TD]3
[/TD]
[TD]4
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]1
[/TD]
[TD]4
[/TD]
[TD]3
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]3
[/TD]
[TD]1
[/TD]
[TD]2
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]3
[/TD]
[TD]1
[/TD]
[TD]4
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]4
[/TD]
[TD]1
[/TD]
[TD]3
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]4
[/TD]
[TD]3
[/TD]
[TD]1
[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
I think I'm going to have to tap out at this point. I actually modified the code to handle the conditions you said, but it was painfully slow. In order to get it to run in a reasonable amount of time, I'd have to write sort routines and other types of optimizations. And I think that would be a lot of work for very little return. Maybe if you explained the ultimate goal of your analysis I could suggest an alternate approach. However, keep in mind that if you're using this to look at lottery numbers or some such thing, this kind of analysis will most certainly not help.
 
Upvote 0
Hi Eric,

I work for a security firm and need to generate multiple rounds for guards.

For instance

I can use their names:
[TABLE="width: 0"]
<tbody>[TR]
[TD]j.green
[/TD]
[TD]1
[/TD]
[TD]1
[/TD]
[TD]1
[/TD]
[/TR]
[TR]
[TD]b.opensheim
[/TD]
[TD]2
[/TD]
[TD]2
[/TD]
[TD]2
[/TD]
[/TR]
[TR]
[TD]a.gardener
[/TD]
[TD]3
[/TD]
[TD]3
[/TD]
[TD]3
[/TD]
[/TR]
[TR]
[TD]d.bryant
[/TD]
[TD]4
[/TD]
[TD]4
[/TD]
[TD]4
[/TD]
[/TR]
[TR]
[TD]m.whitaker
[/TD]
[TD]5
[/TD]
[TD]5
[/TD]
[TD]5
[/TD]
[/TR]
[TR]
[TD]o.stuart
[/TD]
[TD]6
[/TD]
[TD]6
[/TD]
[TD]6
[/TD]
[/TR]
</tbody>[/TABLE]

But it’s easier to use their Id numbers in the first block in place of their names.
[TABLE="width: 0"]
<tbody>[TR]
[TD]5910685
[/TD]
[TD]1
[/TD]
[TD]1
[/TD]
[TD]1
[/TD]
[/TR]
[TR]
[TD]5901763
[/TD]
[TD]2
[/TD]
[TD]2
[/TD]
[TD]2
[/TD]
[/TR]
[TR]
[TD]5903425
[/TD]
[TD]3
[/TD]
[TD]3
[/TD]
[TD]3
[/TD]
[/TR]
[TR]
[TD]5907277
[/TD]
[TD]4
[/TD]
[TD]4
[/TD]
[TD]4
[/TD]
[/TR]
[TR]
[TD]5901322
[/TD]
[TD]5
[/TD]
[TD]5
[/TD]
[TD]5
[/TD]
[/TR]
[TR]
[TD]5902455
[/TD]
[TD]6
[/TD]
[TD]6
[/TD]
[TD]6
[/TD]
[/TR]
</tbody>[/TABLE]


In my examples I was trying to make it simple with using small number

[TABLE="width: 0"]
<tbody>[TR]
[TD]1
[/TD]
[TD]1
[/TD]
[TD]1
[/TD]
[TD]1
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]2
[/TD]
[TD]2
[/TD]
[TD]2
[/TD]
[/TR]
[TR]
[TD]3
[/TD]
[TD]3
[/TD]
[TD]3
[/TD]
[TD]3
[/TD]
[/TR]
[TR]
[TD]4
[/TD]
[TD]4
[/TD]
[TD]4
[/TD]
[TD]4
[/TD]
[/TR]
[TR]
[TD]5
[/TD]
[TD]5
[/TD]
[TD]5
[/TD]
[TD]5
[/TD]
[/TR]
[TR]
[TD]6
[/TD]
[TD]6
[/TD]
[TD]6
[/TD]
[TD]6
[/TD]
[/TR]
</tbody>[/TABLE]

or letters as they are easier to me to explain.
[TABLE="width: 0"]
<tbody>[TR]
[TD]a
[/TD]
[TD]a
[/TD]
[TD]a
[/TD]
[TD]a
[/TD]
[/TR]
[TR]
[TD]b
[/TD]
[TD]b
[/TD]
[TD]b
[/TD]
[TD]b
[/TD]
[/TR]
[TR]
[TD]c
[/TD]
[TD]c
[/TD]
[TD]c
[/TD]
[TD]c
[/TD]
[/TR]
[TR]
[TD]d
[/TD]
[TD]d
[/TD]
[TD]d
[/TD]
[TD]d
[/TD]
[/TR]
[TR]
[TD]e
[/TD]
[TD]e
[/TD]
[TD]e
[/TD]
[TD]e
[/TD]
[/TR]
[TR]
[TD]f
[/TD]
[TD]f
[/TD]
[TD]f
[/TD]
[TD]f
[/TD]
[/TR]
</tbody>[/TABLE]


What my input will look like is below:

[TABLE="width: 0"]
<tbody>[TR]
[TD]5910685
[/TD]
[TD]1
[/TD]
[TD]1
[/TD]
[TD]1
[/TD]
[/TR]
[TR]
[TD]5901763
[/TD]
[TD]2
[/TD]
[TD]2
[/TD]
[TD]2
[/TD]
[/TR]
[TR]
[TD]5903425
[/TD]
[TD]3
[/TD]
[TD]3
[/TD]
[TD]3
[/TD]
[/TR]
[TR]
[TD]5907277
[/TD]
[TD]4
[/TD]
[TD]4
[/TD]
[TD]4
[/TD]
[/TR]
</tbody>[/TABLE]
My largest site has 55 guards between 3 shifts where they have to hit the access points in column B through L which is 11 points. The shifts are broken down to at most 18 guards on 1 shift. This output will help me to randomize their rounds. Having the duplicates removed would be most helpful. Second only the order of 1, 2, 4, versus having 2,1,4, and 2,4,1, and 1,4, 2 and others is helpful because the location of the guard station dictates where they start, I can live with this one, but if there was and easy way to remove it, it would help too.

I really appreciate your help.


I think I'm going to have to tap out at this point. I actually modified the code to handle the conditions you said, but it was painfully slow. In order to get it to run in a reasonable amount of time, I'd have to write sort routines and other types of optimizations. And I think that would be a lot of work for very little return. Maybe if you explained the ultimate goal of your analysis I could suggest an alternate approach. However, keep in mind that if you're using this to look at lottery numbers or some such thing, this kind of analysis will most certainly not help.
 
Upvote 0
Hi Eric,

I work for a security firm and need to generate multiple rounds for guards.

For instance

I can use their names:
[TABLE="width: 0"]
<tbody>[TR]
[TD]j.green[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]b.opensheim[/TD]
[TD]2[/TD]
[TD]2[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]a.gardener[/TD]
[TD]3[/TD]
[TD]3[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]d.bryant[/TD]
[TD]4[/TD]
[TD]4[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]m.whitaker[/TD]
[TD]5[/TD]
[TD]5[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]o.stuart[/TD]
[TD]6[/TD]
[TD]6[/TD]
[TD]6[/TD]
[/TR]
</tbody>[/TABLE]

But it’s easier to use their Id numbers in the first block in place of their names.
[TABLE="width: 0"]
<tbody>[TR]
[TD]5910685[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]5901763[/TD]
[TD]2[/TD]
[TD]2[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]5903425[/TD]
[TD]3[/TD]
[TD]3[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]5907277[/TD]
[TD]4[/TD]
[TD]4[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]5901322[/TD]
[TD]5[/TD]
[TD]5[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]5902455[/TD]
[TD]6[/TD]
[TD]6[/TD]
[TD]6[/TD]
[/TR]
</tbody>[/TABLE]


In my examples I was trying to make it simple with using small number

[TABLE="width: 0"]
<tbody>[TR]
[TD]1[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]2[/TD]
[TD]2[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]3[/TD]
[TD]3[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]4[/TD]
[TD]4[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]5[/TD]
[TD]5[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]6[/TD]
[TD]6[/TD]
[TD]6[/TD]
[/TR]
</tbody>[/TABLE]

or letters as they are easier to me to explain.
[TABLE="width: 0"]
<tbody>[TR]
[TD]a[/TD]
[TD]a[/TD]
[TD]a[/TD]
[TD]a[/TD]
[/TR]
[TR]
[TD]b[/TD]
[TD]b[/TD]
[TD]b[/TD]
[TD]b[/TD]
[/TR]
[TR]
[TD]c[/TD]
[TD]c[/TD]
[TD]c[/TD]
[TD]c[/TD]
[/TR]
[TR]
[TD]d[/TD]
[TD]d[/TD]
[TD]d[/TD]
[TD]d[/TD]
[/TR]
[TR]
[TD]e[/TD]
[TD]e[/TD]
[TD]e[/TD]
[TD]e[/TD]
[/TR]
[TR]
[TD]f[/TD]
[TD]f[/TD]
[TD]f[/TD]
[TD]f[/TD]
[/TR]
</tbody>[/TABLE]


What my input will look like is below:

[TABLE="width: 0"]
<tbody>[TR]
[TD]5910685[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]5901763[/TD]
[TD]2[/TD]
[TD]2[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]5903425[/TD]
[TD]3[/TD]
[TD]3[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]5907277[/TD]
[TD]4[/TD]
[TD]4[/TD]
[TD]4[/TD]
[/TR]
</tbody>[/TABLE]
My largest site has 55 guards between 3 shifts where they have to hit the access points in column B through L which is 11 points. The shifts are broken down to at most 18 guards on 1 shift. This output will help me to randomize their rounds. Having the duplicates removed would be most helpful. Second only the order of 1, 2, 4, versus having 2,1,4, and 2,4,1, and 1,4, 2 and others is helpful because the location of the guard station dictates where they start, I can live with this one, but if there was and easy way to remove it, it would help too.

I really appreciate your help.
 
Upvote 0
Sorry I haven't gotten back sooner. I may not be able to work on this for a while, so I'd recommend opening a new thread. Get some additional eyes on it. Adding to an old thread often isn't a great idea, since people don't always see new posts on old threads.

If you do, try to be a bit more specific in your requirements. Say that column 1 has ID numbers, and the other columns are checkpoints. And you're looking for a way to randomize the routes. Give a few examples of valid results. I'd also recommend just asking for a macro that creates a list of say 10 routes, which you can rerun whenever necessary. There's no need to print them all. You could save the used routes so that they can be excluded in the future, but with millions of routes to choose from, the odds of that happening are vanishingly small.

Good luck!
 
Upvote 0
Eric,

Thank you so much you have been extremely helpful thus far! It is a pain to come up with these unique routes for 17 different contracts. I will take your advice and repost!
 
Upvote 0
Tusharm,

Please help, I need to generate rounds for security guards at multiple stations and I am looking to see if someone can help me using the code below to create a macro that would print only the unique permutations/combinations in sheet 2:

For instance in sheet 1 my input would be the guards ID number in A1 5910685 and B1 would equal 1 and C2 would equal 1 just like below.


[TABLE="class: cms_table, width: 0"]
<tbody>[TR]
[TD]5910685[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]5901763[/TD]
[TD]2[/TD]
[TD]2[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]5903425[/TD]
[TD]3[/TD]
[TD]3[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]5907277[/TD]
[TD]4[/TD]
[TD]4[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]5901322[/TD]
[TD]5[/TD]
[TD]5[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]5902455[/TD]
[TD]6[/TD]
[TD]6[/TD]
[TD]6[/TD]
[/TR]
</tbody>[/TABLE]


You can use 1 instead of the id number 5910685 and 2 instead of 5901763 and so on so the input can be:

[TABLE="class: cms_table, width: 0"]
<tbody>[TR]
[TD]1[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]2[/TD]
[TD]2[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]3[/TD]
[TD]3[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]4[/TD]
[TD]4[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]5[/TD]
[TD]5[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]6[/TD]
[TD]6[/TD]
[TD]6[/TD]
[/TR]
</tbody>[/TABLE]


Here are some examples of Bad output that should not showup in Sheet 2 because there are duplicate numbers in each row:

[TABLE="class: cms_table, width: 0"]
<tbody>[TR]
[TD]1[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]2[/TD]
[TD]2[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]2[/TD]
[TD]3[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]3[/TD]
[TD]5[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]6[/TD]
[TD]4[/TD]
[TD]6[/TD]
[/TR]
</tbody>[/TABLE]


Good output in Sheet 2 would look like this:
[TABLE="class: cms_table, width: 0"]
<tbody>[TR]
[TD]1[/TD]
[TD]2[/TD]
[TD]3[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]4[/TD]
[TD]5[/TD]
[TD]6[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]2[/TD]
[TD]5[/TD]
[TD]6[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]1[/TD]
[TD]2[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]3[/TD]
[TD]5[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]5[/TD]
[TD]4[/TD]
[TD]6[/TD]
[/TR]
</tbody>[/TABLE]


The code below will allow me to specify what row to end on (1,000,000) and how many columns separate the data (4) in sheet 2.


Sub Permute()
Dim ix(100, 1) As Long, rc As Long, m As Long, md As Variant, i As Long, r As Long, c As Long
Dim MyOut() As Variant, mor As Long, cbr As Long

' Set your max output rows here:
mor = 1000000
' Set columns between results here:
cbr = 4

rc = Cells(1, Columns.Count).End(xlToLeft).Column
m = 0
For i = 1 To rc
ix(i, 0) = Cells(Rows.Count, i).End(xlUp).Row
ix(i, 1) = 1
m = IIf(ix(i, 0) > m, ix(i, 0), m)
Next i
md = Range(Cells(1, 1), Cells(m, rc)).Value
ReDim MyOut(1 To mor, 1 To rc)

r = 0
c = 1
Incr:
r = r + 1
If r > mor Then
Sheets("Sheet2").Cells(1, c).Resize(mor, rc).Value = MyOut
r = 1
c = c + rc + cbr
ReDim MyOut(1 To mor, 1 To rc)
End If

For i = 1 To rc
MyOut(r, i) = md(ix(i, 1), i)
Next i

For i = rc To 1 Step -1
ix(i, 1) = ix(i, 1) + 1
If ix(i, 1) <= ix(i, 0) Then GoTo Incr:
ix(i, 1) = 1
Next i

Sheets("Sheet2").Cells(1, c).Resize(mor, rc).Value = MyOut

End Sub


The other thing on my wish list is to have only one of the following show up in my list because they are virtually all the same number (I didn’t include all of the combinations but just a few for 1,2,3,4):


[TABLE="class: cms_table, width: 0"]
<tbody>[TR]
[TD]1[/TD]
[TD]2[/TD]
[TD]3[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]2[/TD]
[TD]4[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]3[/TD]
[TD]2[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]3[/TD]
[TD]4[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]4[/TD]
[TD]3[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]4[/TD]
[TD]2[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]1[/TD]
[TD]3[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]1[/TD]
[TD]4[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]3[/TD]
[TD]1[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]3[/TD]
[TD]1[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]4[/TD]
[TD]1[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]4[/TD]
[TD]3[/TD]
[TD]1[/TD]
[/TR]
</tbody>[/TABLE]



I am ok if the last item can't be done, but it sure would be helpful!
 
Upvote 0

Forum statistics

Threads
1,223,920
Messages
6,175,373
Members
452,638
Latest member
Oluwabukunmi

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