Hi,
Sorry, dass ich mich hier mit reinhäng, aber ich vermute, der Kollege ist auf dem Holzweg. Nix für ungut Gast. :-)
Spielt denn die Sortierung oder Verwendung doppelter Einträge, wie Gast sie beschreibt, überhaupt eine Rolle? Du sprichst in deiner Ausgangsfrage doch nur von Kombinationen und nicht von Variationen. Für 4 aus 20 gibt es exakt 4845 Möglichkeiten, welche dein Code (mit +1 statt +2 pro Schleife) bereits auflistet. Etwas weniger sind es, sobald du deine Regeln mit einer simplen Bedingung eingebaut hast. If b > a + 1 And (c > b + 1 Or d > c + 1) Or c > b + 1 And d > c + 1 Then usw.
Als Code sieht das dann so aus:
Sub Kombinationen()
Dim a%, b%, c%, d%, x&, y&
x = 1
For a = 1 To 20
For b = a + 1 To 20
For c = b + 1 To 20
For d = c + 1 To 20
If y = 65536 Then
y = 0
x = x + 1
End If
If b > a + 1 And (c > b + 1 Or d > c + 1) Or c > b + 1 And d > c + 1 Then
y = y + 1
Cells(y, x) = a & "," & b & "," & c & "," & d & ","
End If
Next d
Next c
Next b
Next a
End Sub
Für den zweiten Schritt brauchst du dann noch eine zweite Zeilen-Variable (z.B. z), die du gleich mit einbauen kannst. Angenommen in der Variable Suchzahl steht die 19 dann kannst du den Code wie folgt erweitern:
Sub Kombinationen()
Dim a%, b%, c%, d%, x&, y&, z&, Suchzahl%
Suchzahl = 19
x = 1
For a = 1 To 20
For b = a + 1 To 20
For c = b + 1 To 20
For d = c + 1 To 20
If y = 65536 Then
y = 0
x = x + 1
End If
If b > a + 1 And (c > b + 1 Or d > c + 1) Or c > b + 1 And d > c + 1 Then
y = y + 1
Cells(y, x) = a & "," & b & "," & c & "," & d
End If
If a = Suchzahl Or b = Suchzahl Or c = Suchzahl Or d = Suchzahl Then
z = z + 1
Cells(z, x + 3) = a & "," & b & "," & c & "," & d
End If
Next d
Next c
Next b
Next a
End Sub
Das sind dann nur noch 969 Treffer.
Gruß Mr. K.
|