help: 4-number permutations

  • Thread starter Thread starter Ng Tian Khean
  • Start date Start date
N

Ng Tian Khean

Can somebody please help me? For a given 4-digit number, I would like to
create a column listing all the possible permutations of it. For example, if
the number is '1102' I would like a column showing 1102, 1012, 1021
..........N. (The formula/function Permut only tells ne how many permutations
of it, not list all the numbers. Please tell me clearly step by step how to
do it. It is for a class project.
Thank you.
 
Here is a VBA solution. The spreadsheet solution is harder.
Sub perm4()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim m As Integer
Dim p As Integer
Dim NN As Integer
Range("A:A").ClearContents
NN = 4 - 1
p = 1
For i = 0 To NN
For j = 0 To NN
For k = 0 To NN
For m = 0 To NN
If Not (i = j Or i = k Or i = m Or j = k Or j = m _
Or k = m) Then
Range("A:A").Cells(p, 1) = i * 1000 + j * 100 + _
k * 10 + m
p = p + 1
End If
Next m
Next k
Next j
Next i
End Sub

Format column A > Custom > 0000
 
Back
Top