Aqui está a versão das listas separadas por vírgulas:
Function nextPerm2(s As String)
' inspired by http://stackoverflow.com/questions/352203/generating-permutations-lazily
' this produces the "next" permutation
' it allows one to step through all possible iterations without having to have them
' all in memory at the same time
Dim L As Integer, ii As Integer, jj As Integer
Dim c() As Variant, temp As Variant
L = Len(s)
If StrComp(s, "**done**") = 0 Or StrComp(s, "") = 0 Then
nextPerm2 = ""
Exit Function
End If
' convert to byte array... more compact to manipulate
arr = Split(s, ",")
ReDim c(1 To UBound(arr) + 1)
For ii = 1 To UBound(arr) + 1
c(ii) = arr(ii - 1)
Next ii
L = UBound(arr) + 1
' find the largest "tail":
For ii = L - 1 To 1 Step -1
If c(ii) < c(ii + 1) Then Exit For
Next ii
' if we complete the loop without break, ii will be zero
If ii = 0 Then
nextPerm2 = "**done**"
Exit Function
End If
' find the smallest value in the tail that is larger than c(ii)
' take advantage of the fact that tail is sorted in reverse order
For jj = L To ii + 1 Step -1
If c(jj) > c(ii) Then
' swap elements
temp = c(jj)
c(jj) = c(ii)
c(ii) = temp
Exit For
End If
Next jj
' now reverse the characters from ii+1 to the end:
nextPerm2 = ""
For jj = 1 To ii
nextPerm2 = nextPerm2 & c(jj) & ","
Next jj
For jj = L To ii + 1 Step -1
nextPerm2 = nextPerm2 & c(jj) & ","
Next jj
If Right(nextPerm2, 1) = "," Then nextPerm2 = Left(nextPerm2, Len(nextPerm2) - 1)
End Function
A análise usa Split()
e há outras alterações.
Não totalmente testado!