QuickSort für ObjectScripting.Dictionary
24.03.2020 15:00:47
Thomas
habe zwei Tage im Forum über QuickSort für CreateObject("Scripting.Dictionary") gesucht und das Ergebnis in folgendem Code erstellt.
Meine Bitte, ob einer der darin erfahreneren VBA-ler das durchschauen kann, ob das sauber definiert ist, bevor ich das in einem größeren Tools einfüge.
Private Sub UserForm_Combobox_Fuellen()
'ComboBoxes füllen ohne doppelte Einträge und sortieren
'Alle Spalten haben die gleiche Anzahl von Einträgen
'Die Werte aus Spalte D, E und R sollen in ComboBoxen: Start_Hlst, SollZeit, und Hpkt _
eingelesen werden:
Dim hsh1 As Object, hsh2 As Object, hsh3 As Object
Dim I As Long, lngLR As Long, vArray As Variant
Set hsh1 = CreateObject("Scripting.Dictionary")
Set hsh2 = CreateObject("Scripting.Dictionary")
Set hsh3 = CreateObject("Scripting.Dictionary")
With Sheets("Tabelle1")
For I = 5 To .Cells(.Rows.Count, 4).End(xlUp).Row 'letzte Zeile in Spalte D
'"I" ist eine Variable die einen Wert aufnehmen soll
'"5" verweist auf die erste Zeile mit Daten (1-4 Datenkopf)
'"Cells" verweist auf die Zellen
'"Rows.Count" ist eine Funktion welche die Anzahl Zeilen im Tabellenblatt zurückgibt
'",4" in welcher Spalte gesucht werden soll (hier D)
'".End" weisst EXCEL an, dass es dort beginnen soll zu zählen
'"xlUp" ist die Richtung in welche EXCEL suchen soll
'".Row" gibt die erste Zeile von unten zurück, wo etwas drinsteht
hsh1(.Cells(I, 4).Text) = 0 ' = Spalte D
hsh2(.Cells(I, 5).Text) = 0 ' = Spalte E
hsh3(.Cells(I, 18).Text) = 0 ' = Spalte R
Next
End With
'hash.keys Einträge: hsh1 , hsh2 und hsh3 sortieren
vArray = hsh1.keys
Call QuickSort(vArray, 0, UBound(vArray))
hsh_1 = vArray
vArray = hsh2.keys
Call QuickSort(vArray, 0, UBound(vArray))
hsh_2 = vArray
vArray = hsh3.keys
Call QuickSort(vArray, 0, UBound(vArray))
hsh_3 = vArray
UserForm2.cmb_Start_Hlst.List = Application.Transpose(hsh_1)
UserForm2.cmb_SollZeit.List = Application.Transpose(hsh_2)
UserForm2.cmb_Hpkt.List = Application.Transpose(hsh_3)
UserForm2.Show
Unload UserForm2
Set hsh1 = Nothing
Set hsh2 = Nothing
Set hsh3 = Nothing
End Sub
Private Sub QuickSort(vArray As Variant, inLow As Long, inHi As Long)
Dim pivot As Variant
Dim tmpSwap As Variant
Dim tmpLow As Long
Dim tmpHi As Long
tmpLow = inLow
tmpHi = inHi
pivot = vArray((inLow + inHi) \ 2)
While (tmpLow inLow)
tmpHi = tmpHi - 1
Wend
If (tmpLow
Danke schon mal im VorausGruß Thomas