0

in cell "A1" = 123 and "A2" = 456.

May I know how can I show all the possible combination so that it will look like this:

123
132
213
231
312
321
456
465
546
564
645
654

I tried to search online but cannot find any solution. Any help will be appreciate.

1 Answer 1

1

You can use the function below to generate all permutations of the digits in the string.

Sub GetPermutation(x As String, y As String, ByRef CurrentRow As Double)
    Dim i As Integer, j As Integer
    j = Len(y)
    If j < 2 Then
        Cells(CurrentRow, 1) = x & y
        CurrentRow = CurrentRow + 1
    Else
        For i = 1 To j
            Call GetPermutation(x + Mid(y, i, 1), _
            Left(y, i - 1) + Right(y, j - i), CurrentRow)
        Next
    End If
End Sub

For your example you could do:

CurrentRow = 2
Call GetPermutation("",Range("A1").value,CurrentRow)
Sign up to request clarification or add additional context in comments.

1 Comment

If you are planning on using this on large scale problems such as permutations of 9 digits, you shouldn't use Excel. I would suggest something like Python instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.