Generate all possible permutations given input values

3

2

Please note my question is different from the suggested duplicate. The suggested duplicate does not show all possible variations based on the number of options, so an input of 3 can have a variation of 1, 2, and 3 of the inputs. The suggested duplicate always provides 3 inputs per variation.

Furthermore, my question also asks for variations to show all possible orders of the inputs too. The suggested duplicate always shows column A at the left most position of the output, column b in the middle of the output and column c at the left of the output.

Therefore, the suggested duplicate does not answer my question.


Original Question

How do I go about creating a formula which takes any number of given input values, and then generates an output value with all possible combinations permutations based on the input values given.

For example, if the input values where as following

One
Two

The output generated should be something like this

One
OneTwo
Two
TwoOne

Here is what it should look like if 3 values are given:

enter image description here

Does anyone know how to do this in Excel?

I would like to be able to type in any number of input values and the input values will be any word, phrase, number, letters, or a combination permutation of all of those.

I have given this a try https://www.ozgrid.com/forum/forum/help-forums/excel-vba-macros/146983-all-possible-combinations-from-single-column but it does not seem to work.

In column A, if I type (one word per row):

One
Two
Three

In column B the VBA gives me (in a single cell):

one,two,three

It doesn't give me all possible combinations permutations, it just changes what I typed in vertically down a column, and outputs the results horizontally.

oshirowanen

Posted 2019-01-14T21:33:35.920

Reputation: 1 858

1@cybernetic.nomad It looks like the Poster wants all the permutations associated with each combination – Gary's Student – 2019-01-15T00:16:44.270

ORDER is important. in the linked question, order is not important. – Forward Ed – 2019-01-15T00:24:19.293

2Because there are a finite number of rows (1 048 576) to excel you will be limited to only 9 entries if you wish all the permutations to be written out in a single column. If I did my math right, you will be looking at 986 409 permutations. 10 entries would give 9 864 100 which exceeds the number of available rows. – Forward Ed – 2019-01-15T00:27:58.487

1I'd be looking at nested loops in vba. Start by writing out all the single entries, then repeat the process starting with the first entry, and loop through the remaining letters, then bump to the next entry and loop through the remaining letters until all the 2 combinations are written. just keep repeating the pattern and eventually all will be written out. – Forward Ed – 2019-01-15T00:31:05.290

@ForwardEd, in that case, the output doesn't have to go vertically, if the output all goes into a single cell separated by , and | as in one | one,two | two | two,one , that would work for me too. – oshirowanen – 2019-01-16T19:07:55.423

There is also a limitation to the number of Characters in a cell of a little over 32K. You will get further using a column than you will putting it into a single cell. Especially if each entry is more than 1 character long. – Forward Ed – 2019-01-16T23:41:30.110

another option you have is write out ever permutation and combination, and then delete the entries that have repeated entries. Though you will wind with fewer permissible entries as you will hit your row limit sooner due to the rows with repeats. – Forward Ed – 2019-01-16T23:43:53.037

@cybernetic.nomad, you are correct, I am after all permutations. I will try rewording the question. – oshirowanen – 2019-01-17T09:34:51.297

Why does it have to be a formula rather than VB? Being able to use actual loops you would be able to refer to common posts about permutation calculation for other langues. For the macro you just state it doesn't work but not what actually isn't working about it.

– Seth – 2019-01-17T10:06:43.523

@Seth, I have stated that the macro does not work because when in input one two three, it outputs one, two, three. It does not output all permutations. It does not have to be a formula, if it is VB, or even C#, I would be okay with that. – oshirowanen – 2019-01-17T18:47:25.713

1

Here's a screen shot in case it rings a bell for an idea with any Excel functions or add-on features people may be familiar with: https://i.imgur.com/zTh37v6.png Perhaps there's a PowerShell that can do this, if I knew of something I'd try but this should be easy with a relation type query or join of the data in a table object or something to itself if applicable with some of those features.

– Pimp Juice IT – 2019-01-18T01:53:05.017

Answers

4

The following VBA script created for me this spreadsheet:

enter image description here

This is the script:

Option Explicit

Sub PermutationsN()
Dim vElements As Variant, vresult As Variant
Dim lRow As Long, i As Long

vElements = Application.Transpose(Range("A1", Range("A1").End(xlDown)))
Columns("B:Z").Clear

For i = 1 To UBound(vElements)
    ReDim vresult(1 To i)
    Call PermutationsNPR(vElements, i, vresult, lRow, 1)
Next i
End Sub

Sub PermutationsNPR(vElements As Variant, p As Long, vresult As Variant, lRow As Long, iIndex As Integer)
Dim i As Long, unique As Variant

For i = 1 To UBound(vElements)
    vresult(iIndex) = vElements(i)
    If iIndex = p Then
        unique = UniqueArray(vresult)
        If (UBound(vresult) = UBound(unique)) Then
            lRow = lRow + 1
            Cells(lRow, 3).Value = Join(unique)
        End If
    Else
        Call PermutationsNPR(vElements, p, vresult, lRow, iIndex + 1)
    End If
Next i
End Sub


Function UniqueArray(todoarray As Variant) As Variant
  Dim arr As New Collection, a
  Dim i As Long
  On Error Resume Next
  For Each a In todoarray
     arr.Add a, a
  Next
  ReDim returnVal(1 To arr.count)
  For i = 1 To arr.count
     returnVal(i) = arr(i)
  Next
  UniqueArray = returnVal
End Function

The macro will work for any number of items in the A column, within Excel limits.

harrymc

Posted 2019-01-14T21:33:35.920

Reputation: 306 093

Very nice script Harry +1. I tested and confirmed that this does exactly what is requested. You must have worked hard for the money on this one sir, nice job!! – Pimp Juice IT – 2019-01-18T17:41:19.750

2

consider this example A column data:- Numbers(header) one two three four five B column data:- OutPut(header)

paste this formula in B2 :-

=IFERROR(IF(INDEX(OFFSET($A$1,1,0,COUNTA($A$2:$A$20)),ROUNDUP(ROWS($A$1:A1)/COUNTA($A$2:$A$20),))=INDEX(OFFSET($A$1,1,0,COUNTA($A$2:$A$20)),MOD(ROWS($A$1:A1)-1,COUNTA($A$2:$A$20))+1),INDEX(OFFSET($A$1,1,0,COUNTA($A$2:$A$20)),ROUNDUP(ROWS($A$1:A1)/COUNTA($A$2:$A$20),)),INDEX(OFFSET($A$1,1,0,COUNTA($A$2:$A$20)),ROUNDUP(ROWS($A$1:A1)/COUNTA($A$2:$A$20),))&INDEX(OFFSET($A$1,1,0,COUNTA($A$2:$A$20)),MOD(ROWS($A$1:A1)-1,COUNTA($A$2:$A$20))+1)),"")

this function works "Numbers" column having data upto A20.

permutationexcelimage

vivek

Posted 2019-01-14T21:33:35.920

Reputation: 66

Your formula just gives the permutations for the terms taken one and two at a time, not all permutations. It's possible to do this kind of formula for a specific number of terms, although it quickly gets insane. Even at three entries, the formula is outrageous. See Forward Ed's comment about 9 entries being all that would fit in Excel's row limit, which tells you that if your formula goes up to 19 entries, it's not doing what was asked in the question. – fixer1234 – 2019-01-18T10:25:50.003