Prev: Tom Shelton ...
Next: Instr problem in VB5
From: Larry Serflaten on 3 Jun 2010 08:19 "Dave O." <nobody(a)nowhere.com> wrote > you must ignore permutations where (in this example) j, k or m are the same, Aaah, something of a scrabble shuffler then.... Try this with a listbox on a new form: LFS Private Sub Form_Load() Dim itm For Each itm In Shuffle("ABCDEF", 4) List1.AddItem itm Next End Sub Function Shuffle(List As String, Length As Long) As Collection Dim max As Long, idx As Long, ltr As Long Dim txt As String, use As Boolean ' Returns a collection of strings that are <Length> characters long ' containing all combinations of unique characters from <List> max = Len(List) ReDim p(0 To Length) As Long Set Shuffle = New Collection txt = String(Length, " ") For idx = 1 To Length p(idx) = 1 Next idx = Length While idx > 0 use = True ReDim c(1 To max) As Long For ltr = 1 To Length Mid(txt, ltr, 1) = Mid(List, p(ltr), 1) c(p(ltr)) = c(p(ltr)) + 1 If c(p(ltr)) > 1 Then use = False Exit For End If Next If use Then Shuffle.Add txt idx = Length p(idx) = p(idx) + 1 While p(idx) > max p(idx) = 1 idx = idx - 1 p(idx) = p(idx) + 1 Wend Wend End Function |