# All possible permutation/combinations

#### Dr. Demento

##### Well-known Member
Good morning!

Forgive me, but I don't remember the difference between permutations and combinations, so hopefully my explanation points in the proper direction.

I have two ordered lists (NORTH|SOUTH|EAST|WEST) and (N|S|E|W) and I want to make a listing of all permutations/combinations?? substituting N for NORTH, S for SOUTH, etc. The order of the values is fixed as shown.

For example, if I start with the first row, I want all possible perms/combos with both the full word and the abbreviation, as shown with the next four lines.

|-------|-------|-------|-------|
| NORTH | SOUTH | EAST | WEST |
|-------|-------|-------|-------|
| N | SOUTH | EAST | WEST |
|-------|-------|-------|-------|
| N | S | EAST | WEST |
|-------|-------|-------|-------|
| N | S | E | WEST |
|-------|-------|-------|-------|
| N | S | E | W |
|-------|-------|-------|-------|

This is just a partial example; I was trying to manually jam this out and kept getting screwed up.

Any help would be much appreciated.

Thanks y'all.

#### Dr. Demento

##### Well-known Member
Yes @johnnyL that is exactly right.
There are only 16 answers; it's a substitution permutation. Fluff & Eric W show the proper answers.

### 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.

#### johnnyL

##### Well-known Member
So it's not a permutation as I understand permutation.

When I think of permutation I think of it like a phone number, in your case, it would be only 4 long though.
If you were to "dial" 'NESW' well that would be different than 'NSEW'.

In your case you have 8 different items taken 4 at a time. The excel formula for that is
=PERMUT(8,4)
which equates to 1680.

I guess I wasn't in tune to what you were looking for.

#### Dr. Demento

##### Well-known Member
You're correct. I couldn't remember which was which, but it appears I was looking for a substitution combination.

Appreciate it.

#### Fluff

##### MrExcel MVP, Moderator
With the same setup as post#9 how about
VBA Code:
``````Sub DrDemento()
Dim Rng As Range
Dim r As Long, c As Long
Dim a As Variant, b As Variant

Set Rng = Range("A1:D2")
r = 2
c = 4
a = Evaluate("mid(base(transpose(row(1:" & r ^ c & ")-1)," & r & "," & c & "),row(1:" & c & "),1)+1")
b = Application.Index(Rng.Value, Application.Transpose(a), Array(1, 2, 3, 4))
Range("A5").Resize(UBound(b), UBound(b, 2)).Value = b
End Sub``````

#### Dr. Demento

##### Well-known Member
Thank you, Fluff, it doesn't seem to work.

b is a single dimension array and when i change UBound(b, 2) to UBound(b) in the final line, I get four copies of the top row. I understand why I get that result but I'm not sure what a and b do.

Thanks much.

d

#### Fluff

##### MrExcel MVP, Moderator
If you step through the code when you get to the line `b = Application.Index` have a look n the locals window & is a an array 1 to 4, 1 to 16?

No, a = 1

#### Fluff

##### MrExcel MVP, Moderator
Thanks for that, it looks as though the evaluate isn't working in your version. Try it like this instead
VBA Code:
``````Sub DrDemento()
Dim rng As Range
Dim r As Long, c As Long
Dim a As Variant, b As Variant

Set rng = Range("A1:D2")
r = 2
c = 4
a = Evaluate("if({1},mid(base(transpose(row(1:" & r ^ c & ")-1)," & r & "," & c & "),row(1:" & c & "),1)+1)")
b = Application.Index(rng.Value, Application.Transpose(a), Array(1, 2, 3, 4))
Range("A5").Resize(UBound(b), UBound(b, 2)).Value = b
End Sub``````

#### Dr. Demento

##### Well-known Member
That works awesome, Fluff! Thanks so much!!

Out of curiosity, how do I convert your subt to use a two 1D arrays (or a single 2D array) as input instead of a range?

Thanks again.

#### Worf

##### Well-known Member
This thread may be of interest:

Replies
0
Views
72
Replies
12
Views
984
Replies
2
Views
126
Replies
3
Views
481
Replies
4
Views
300

1,181,953
Messages
5,932,931
Members
436,869
Latest member
ABGTH

### 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.

### Which adblocker are you using?

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

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