Samgraphics
Board Regular
- Joined
- Jan 9, 2022
- Messages
- 52
- Office Version
- 2011
- Platform
- MacOS
Hi, I've been using this macro code to generate all combinations from a list I provide for a while and it works great, but now I need it to do something a little different because when the combinations gets to a lot it freezes and runs out of memory. Here's the code
I would like to alter the code so that i can tell it where to start and end for example if it's listing all combinations that start with one I would like to be able to make it stop when it gets to two or be able to make it start at two and list only the combinations that start with two.
Please help me.
Thank you.
VBA Code:
Public result() As Variant
Function Combinations(rng As Range, n As Single)
Dim b As Single
rng1 = rng.Value
b = WorksheetFunction.Combin(UBound(rng1, 1), n)
ReDim result(b, n - 1)
Call Recursive(rng1, n, 1, 0, 0)
For g = 0 To UBound(result, 2)
result(UBound(result, 1), g) = ""
Next g
Combinations = result
End Function
Function Recursive(r As Variant, c As Single, d As Single, e As Single, h As Single)
Dim f As Single
For f = d To UBound(r, 1)
result(h, e) = r(f, 1)
If e = (c - 1) Then
For g = 0 To UBound(result, 2)
result(h + 1, g) = result(h, g)
Next g
h = h + 1
Else
Call Recursive(r, c, f + 1, e + 1, h)
End If
Next f
End Function
I would like to alter the code so that i can tell it where to start and end for example if it's listing all combinations that start with one I would like to be able to make it stop when it gets to two or be able to make it start at two and list only the combinations that start with two.
Please help me.
Thank you.