View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Randomly sorting a group of 5 values multiple times

Here is an adaptation of code found a John Walkenbach's site:

Dim CurrentRow

Sub GetString()
Dim InString As String
InString = "abcde"
ActiveSheet.Columns(1).Clear
CurrentRow = 1
Call GetPermutation("", InString)

End Sub

Sub GetPermutation(x As String, y As String)
' The source of this algorithm is unknown
Dim i As Integer, j As Integer
j = Len(y)
If j < 2 Then
'Cells(CurrentRow, 1) = x & y
For i = 1 To 5
Cells(CurrentRow, i) = Mid(x & y, i, 1)
Next
CurrentRow = CurrentRow + 1
Else
For i = 1 To j
Call GetPermutation(x + Mid(y, i, 1), _
Left(y, i - 1) + Right(y, j - i))
Next
End If
End Sub

-----------------------
http://www.j-walk.com/ss/excel/tips/tip46.htm
----------------------

--
Regards,
Tom Ogilvy

"Skylara" wrote in message
...
Hi:

I would like to generate multiple rows of the following variables without
repeating any variable in a particular row. For example: a,c,e,d,b (first
row); b,d,c,e,a (second row); etc. The concept is simple, however it seems
impossible in practice.

Thank you.
--
Skylar