If you use a recursive function, you don't have to write much code at all.Originally posted by TGR
How would anyone have time to write all those if statements in a 2 hr comp? Now that I've seen a solution i understand a bit on how to do it. Thanks for the help!
Here is a general solution that calculates all permutations for any number of inputs:
VB Code:
Sub Permutations(s As String, Optional sleading As String) ' Nucleus Dim i As Long Dim a As Variant a = Split(s, ",") For i = 0 To UBound(a) If UBound(a) > 1 Then Permutations Right$(s, Len(s) - InStr(1, s, ",")), IIf(Len(sleading), sleading & "," & Left$(s, InStr(1, s, ",") - 1), Left$(s, InStr(1, s, ",") - 1)) Else Debug.Print sleading & "," & s End If s = Right$(s, Len(s) - InStr(1, s, ",")) & "," & Left$(s, InStr(1, s, ",") - 1) Next End Sub
To use the function pass csv input values and the function does the rest. Output is to the debug window.
For example to calculate all permutations for 1 2 10?
VB Code:
Call Permutations("1,2,10")




Reply With Quote