montecarlo2012
Well-known Member
- Joined
- Jan 26, 2011
- Messages
- 986
- Office Version
- 2010
- Platform
- Windows
Hello.
This code return (" all ") the permutation,
I want to control "wich" permutation
example.
and the total.
the result of adding, here I separate manually whatever add 5, (I would like to control that)
also, you can see there are 2, and 1,1,1 and 0's
what about if I need the 2, 2, 1 and 0's only
How is possible to add this filters
Thank you for reading this
VBA Code:
Sub ListPermut()
'This macro creates a list of all permutations
'Define variables
Dim ws As Worksheet, Ans As String, ans1() As String, digits As Integer
Dim num As Integer, p As Long, i As Long, t As Long
Dim rng() As Long, c As Long, rng1() As String
'Ask for user input
Ans = InputBox("Type strings separated with a comma:")
digits = InputBox("How many strings?")
'Split text strings to an array
ans1 = Split(Ans, ",")
'Count values in aray
num = UBound(ans1) + 1
'Calculate number of permutations
p = num ^ digits
'Redimension arrays
ReDim rng(1 To digits)
ReDim rng1(1 To digits)
'Save 1 to all values in first row of array
For c = 1 To digits
rng(c) = 1
Next c
i = 0
'Don't show the result until finished
Application.ScreenUpdating = False
'Repeat until all permutations have been created
Do Until (i + t) = (p - 1)
'Use text strings instead of numbers
For c = LBound(rng1) To UBound(rng1)
rng1(c) = ans1(rng(c) - 1)
Next c
'Transfer values from array to worksheet
Sheet1.Range("A1").Resize(, digits).Offset(i) = rng1
'Build next row of permutations
For c = digits To 1 Step -1
If c = digits Then
rng(c) = rng(c) + 1
ElseIf rng(c) = 0 Then
rng(c) = rng(c - 1)
End If
If rng(c) = num + 1 Then
rng(c) = 1
rng(c - 1) = rng(c - 1) + 1
End If
Next c
'Count made permutations
i = i + 1
'Insert a new sheet if rows exceed 999 999
If i = 1000000 Then
'Set ws = Sheets.Add
t = t + 1000000
i = 0
End If
Loop
'Use text strings instead of numbers
For c = LBound(rng1) To UBound(rng1)
rng1(c) = ans1(rng(c) - 1)
Next c
'Transfer values from array to worksheet
Sheet1.Range("A1").Resize(, digits).Offset(i) = rng1
'Show output
Application.ScreenUpdating = True
End Sub
I want to control "wich" permutation
example.
and the total.
the result of adding, here I separate manually whatever add 5, (I would like to control that)
also, you can see there are 2, and 1,1,1 and 0's
what about if I need the 2, 2, 1 and 0's only
How is possible to add this filters
Thank you for reading this