Permutation in Excel

ygilbert

New Member
Joined
Jul 12, 2008
Messages
44
I have the following:

(A,B,C,D) and (1,2,3) and (X,Y,Z)

So I would like to get the following combinations

A1X,A1Y,A1Z, A2X,A2Y,A2Z, ..., D3X,D3Y,D3Z

How is this done in excel?
 
Ok, thanks again Cindy I will work it out from here. If you think of anymore which may help with this recalculation bit as it is very important please post.

Anybody like to try for a formula for this? It is way beyond me :rolleyes:

Much appreciated
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Is there a way to get a column to cycle through all combinations for one column, but exclude that for others and only take 1 variable. For ex. let's take just column a and b. I woulid want to know this. A+all the combinations withing B. so, the combinations in this instance are

A1&B1
A1&B2
A1&B3
A1&B1&B2
A1&B1&B3
A1&B2&B3

A2&B1
A2&B2
A2&B3
A2&B1&B2
A2&B1&B3
A2&B2&B3

No repeats and I wouldn't need the A1+A2 Combination as those are mutually exclusive, while the other columns can be inclusive of all elements in said column.

So some columns I ONLY want one of the variables, and others, I'd want to cycle through all combinations of variables within a column.

DOes this make sense?
 
Upvote 0
Hi

Another option, using vba.

This should work with any number of sets, each with any number of elements.

Write the sets in contiguous columns, starting in column A. Write each set in contiguous rows starting in row 1. Leave the column after the last set empty.

Try:

Code:
Sub Perm()
Dim rSets As Range, rOut As Range
Dim vArr As Variant, lrow As Long
 
Set rSets = Range("A1").CurrentRegion
ReDim vArr(1 To rSets.Columns.Count)
Set rOut = Cells(1, rSets.Columns.Count + 2)
Perm1 rSets, vArr, rOut, 1, lrow
End Sub
 
Sub Perm1(rSets As Range, ByVal vArr As Variant, rOut As Range, ByVal lSetN As Long, lrow As Long)
Dim j As Long
 
For j = 1 To rSets.Rows.Count
    If rSets(j, lSetN) = "" Then Exit Sub
    vArr(lSetN) = rSets(j, lSetN)
    If lSetN = rSets.Columns.Count Then
        lrow = lrow + 1
        rOut(lrow).Resize(1, rSets.Columns.Count).Value = vArr
    Else
        Perm1 rSets, vArr, rOut, lSetN + 1, lrow
    End If
Next j
End Sub

Ex:


ABCDEFGHIJ
1a1b1c1d1a1b1c1d1
2a2b2c2d2a1b1c1d2
3b3c3a1b1c2d1
4a1b1c2d2
5a1b1c3d1
6a1b1c3d2
7a1b2c1d1
8a1b2c1d2
9a1b2c2d1
10a1b2c2d2
11a1b2c3d1
12a1b2c3d2
13a1b3c1d1
14a1b3c1d2
15a1b3c2d1
16a1b3c2d2
17a1b3c3d1
18a1b3c3d2
19a2b1c1d1
20a2b1c1d2
21a2b1c2d1
22a2b1c2d2
23a2b1c3d1
24a2b1c3d2
25a2b2c1d1
26a2b2c1d2
27a2b2c2d1
28a2b2c2d2
29a2b2c3d1
30a2b2c3d2
31a2b3c1d1
32a2b3c1d2
33a2b3c2d1
34a2b3c2d2
35a2b3c3d1
36a2b3c3d2
37
[Book1]Sheet1

<tbody>
</tbody>

Hello,

I realize this post and thread are quite old, but I am hopeful I may find some additional help on it. I am trying to do something similar to what this script does, but with one slight difference and I have searched quite a bit with no luck. What if column B (or C) happened to have a duplicate of an entry in column A, and I want to generate all permutations without any that have a duplicate from one column to another. For example let's say column A and B both contain "a2"...is there a script to be able to list all permutations without any that would include "a2 a2 c1 d1" and so on? To further clarify, if i have 4 or 5 columns of names and some names appear in multiple columns, and I want the permutations of all names, but don't need the same name to appear twice in a row, can that be done? I also would like to have a header row if possible and be able to do what the above script does in terms of any amount of columns or names. Thank you very much in advance for any help.
 
Upvote 0

Forum statistics

Threads
1,215,430
Messages
6,124,849
Members
449,194
Latest member
HellScout

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