Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1352to1356
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

Permutationen in Mappe schreiben

Permutationen in Mappe schreiben
06.03.2014 11:40:52
Dogbert
Hallo!
Ich möchte in eine Excelmappe die Permutationen aller Zahlen von 1 bis n schreiben.
Dazu habe ich einen VBA-Code gebastelt (siehe unten). Dieser funktioniert auch.
Allerdings möchte ich die Anzahl an Zahlen, die verknüpft werden sollen, variabel halten. Es sollen statt vier Zahlen also auch z.B. 3, 6 oder 9 möglich sein.
Hat jemand eine Lösung, wie ich die Anzahl variabel gestalten kann?
Mir ist bewusst, dass die Kombinationen schnell ansteigen und jede Menge Zeile gebraucht werden können. :-)
Danke!

Sub befuellen()
Dim a, b, c, d As Integer
Dim zeile As Double
zeile = 1
For a = 1 To 4
zeilefertig = False
For b = 1 To 4
If b  a Then
For c = 1 To 4
If (c  a) And (c  b) Then
For d = 1 To 4
If (d  a) And (d  b) And (d  c) Then
zeile = zeile + 1
Cells(zeile, 2) = a
Cells(zeile, 3) = b
Cells(zeile, 4) = c
Cells(zeile, 5) = d
End If
Next d
End If
Next c
End If
Next b
Next a
End Sub

3
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Permutationen in Mappe schreiben
06.03.2014 12:28:06
Rudi
Hallo,
Sub bbbb()
Dim iAnzahl As Integer, iCounter As Integer, lngAnzElemente As Long
Dim arrValues()
Dim objErg As Object
Dim strErg As String
Dim arrKeys, arrErg(), i As Long
Set objErg = CreateObject("Scripting.dictionary")
iAnzahl = Application.InputBox("Anzahl?", , , , , , , 1)
lngAnzElemente = WorksheetFunction.Fact(iAnzahl)
If lngAnzElemente > Rows.Count Then
MsgBox "Zu viele Elemente!" _
& vbLf & Format(lngAnzElemente, "#,##0") _
& " Elemente", vbOKOnly, "Gebe bekannt ..."
Exit Sub
End If
ReDim arrValues(iAnzahl - 1)
For iCounter = 1 To iAnzahl
arrValues(iCounter - 1) = iCounter  'Chr(64 + iCounter)
Next
Kombiniere arrValues, "", objErg
arrKeys = objErg.keys
ReDim arrErg(1 To objErg.Count, 1 To 1)
For i = 0 To UBound(arrKeys)
arrErg(i + 1, 1) = arrKeys(i)
Next
Sheets.Add.Cells(1, 1).Resize(objErg.Count) = arrErg
MsgBox "Job erledigt!" & vbLf & objErg.Count & " Elemente", vbOKOnly, "Gebe bekannt ..."
End Sub

Sub Kombiniere(arrValues, strErg, objErg)
'alle möglichen Kombinationen
Dim i%, j%, k%
Dim strTmp, arrTmp
If UBound(arrValues) > 0 Then
ReDim arrTmp(UBound(arrValues) - 1)
For i = 0 To UBound(arrValues)
strTmp = strErg & arrValues(i)
k = 0
For j = 0 To UBound(arrValues) - 1
If i = j Then k = 1
arrTmp(j) = arrValues(j + k)
Next j
Kombiniere arrTmp, strTmp, objErg
Next i
Else
objErg(strErg & arrValues(0)) = 0
End If
End Sub

Gruß
Rudi

Anzeige
AW: Permutationen in Mappe schreiben
06.03.2014 12:52:46
Dogbert
Vielen Dank!

Links zu Excel-Dialogen

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige