Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

All Possible combination in VBA

Status
Not open for further replies.

bontle01

Programmer
Oct 10, 2008
7
ZA
How do i code vba to give me all possible combination of 3 combination 5?
Masese
 





Hi,

Would you care to explain what you mean by "3 combination 5"

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
I meant 5C3. That is the number of possioble way of choosing 3 letter from a group of 5 letters.
 




What code or logic do you have so far?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 




Well, my friend, you have to start somewhere! No one, here is going to hand you a gimme.

Ball's in your court.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Sub Combos()


Dim Total As Integer
Dim N1 As Integer
Dim N2 As Integer, Sum2 As Integer
Dim N3 As Integer, Sum3 As Integer
Dim N4 As Integer, Sum4 As Integer
Dim N5 As Integer, Sum5 As Integer
Dim N6 As Integer
Dim iRow As Long

Total = 100
'Total = CInt(InputBox("Enter desired total", "Combos", 60))
If Total = 0 Then Exit Sub

iRow = 1
'For N6 = (Total + 15) \ 6 To 49
'For N6 = (Total + 15) \ 6 To 100
For N6 = Total \ 3 To 100
For N5 = 5 To N6 - 1
Sum5 = N5 + N6
For N4 = 4 To N5 - 1
'For N4 = 1 To N5 - 1
Sum4 = Sum5 + N4
'For N3 = 3 To N4 - 1
'Sum3 = Sum4 + N3
' For N2 = 2 To N3 - 1
' Sum2 = Sum3 + N2
'N1 = Total - Sum2
'If N1 < 1 Then Exit For
'If N1 < N2 Then
iRow = iRow + 1
'Cells(iRow, 1) = N1
'Cells(iRow, 2) = N2
'Cells(iRow, 3) = N3
Cells(iRow, 4) = N4
Cells(iRow, 5) = N5
Cells(iRow, 6) = N6
'End If
'Next N2
'Next N3
Next N4
Next N5
Next N6

MsgBox iRow & " combinations found.", vbInformation, "Combos"

End Sub

Sub Iteration()
Dim a, b, c, d, e, f, g, h, i, j As Integer
For a = 1 To 98
For b = 1 To 98
For c = 1 To 98
' For d = 0 To 100
'For e = 0 To 100
'For f = 0 To 100
'For g = 0 To 100
' For h = 0 To 100
If a + b + c = 100 Then
For i = 2 To 50
For j = 2 To 50
Cells(i, j) = a * b * c
Next j
Next i

End If

'Next h
' Next g
'Next f
' Next e
' Next d
Next c
Next b
Next a



End Sub
 
In Excel you can use COMBIN().
The formula is n!/k!(n-k)!
 





Wow, that was fast, for having "none" a mere 8 minutes ago! ;-)

So what is the problem with the this code?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top