• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Permutations and Combinations

kmzach

New Member
Can someone give the VB code for a macro to get all possible combinations of n items taking r items from it at a time? I am looking for the code to list all the nCr combinations
 
How's this? Note that there is a limit as to how big a number you can calculate...

[pre]
Code:
Function Combos(n As Integer, r As Integer) As Double

If n < 1 Or r < 1 Then
Combos = 0
Exit Function
End If

Combos = 1

For i = 1 To r
'In Case n is less than r
Do While n > 0
Combos = Combos * n
n = n - 1
Loop
Next

End Function
[/pre]
 
Back
Top