Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1104to1108
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

ComboBox schneller sortieren

ComboBox schneller sortieren
otto
Hi,
mit folgendem Code (aus dem Forum) sortiere ich derzeit 4 Comboboxen mit je ca. 2000 - 5000 Einträgen
Das ganze dauert ungefähr 25-30 sec. Geht das irgendwie schneller?
With Kundenauftrag.ComboBox8
For Letzter = 0 To .ListCount - 1
For Naechster = Letzter + 1 To .ListCount - 1
If .List(Letzter) > .List(Naechster) Then
i = .List(Letzter)
.List(Letzter) = .List(Naechster)
.List(Naechster) = i
End If
Next Naechster
Next Letzter
End With
Gruß
otto

12
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Benutzer
Anzeige
AW: ComboBox schneller sortieren
23.09.2009 12:33:56
Wolli
Hallo Otto, das ist ein sehr simples, langsames Sortierverfahren. Ich persönlich würde wohl die Liste in ein Tabellenblatt schreiben, darin mit range.sort sortieren und wieder auslesen.
Alternativ kannst Du nach besseren Sortierverfahren suchen, da gibt es unendlich viele Ansätze. Beispiele: Quicksort, Bubblesort. Googlen bringt dir die Algorithmen dazu, sie müssen dann nur für Deine Bedürfnisse angepasst werden.
Ich lasse den Thread nochmal offen.
Gruß, Wolli
AW: ComboBox schneller sortieren
23.09.2009 12:35:53
Rudi
Hallo,
erst in ein Array einlesen, per QuickSort sortieren und wieder zurückschreiben.
With Kundenauftrag.ComboBox8
arrTmp=.List
Quicksort arrTmp
.clear
.list=arrTmp
end with
Sub QuickSort(ByRef VA_Array, Optional V_Low1, Optional V_High1)
On Error Resume Next
Dim V_Low2 As Long, V_High2 As Long
Dim V_Val1, V_Val2 As Variant
If IsMissing(V_Low1) Then
V_Low1 = LBound(VA_Array, 1)
End If
If IsMissing(V_High1) Then
V_High1 = UBound(VA_Array, 1)
End If
V_Low2 = V_Low1
V_High2 = V_High1
V_Val1 = VA_Array((V_Low1 + V_High1) / 2)
While (V_Low2  V_Val1 And _
V_High2 > V_Low1)
V_High2 = V_High2 - 1
Wend
If (V_Low2  V_Low1) Then Call _
QuickSort(VA_Array, V_Low1, V_High2)
If (V_Low2 

Gruß
Rudi
Anzeige
AW: ComboBox schneller sortieren
23.09.2009 12:50:19
otto
Hi,
bleibt immer bei
V_Low2 = V_Low2 + 1 hängen.
muss noch irgendwas deklariert werden? (z.B. VA_Array)
otto
AW: ComboBox schneller sortieren
23.09.2009 12:42:30
Tino
Hallo,
teste mal diese Version
Beispiel
Option Explicit 
 
 
Sub Beispiel() 
Dim MeinArray() 
MeinArray = Kundenauftrag.ComboBox8.List 
prcQuickSort Lbound(MeinArray), Ubound(MeinArray), 0, True, MeinArray 
Kundenauftrag.ComboBox8.List = MeinArray 
 
End Sub 
 
kommt als Code in Modul
Option Explicit 
 
'   Code Max Kaffl 2005 
 
Sub prcQuickSort(lngLbound As Long, lngUbound As Long, _
    intSortColumn As Integer, bntSortKey As Boolean, vntArray() As Variant) 
    Dim intIndex As Integer 
    Dim lngIndex1 As Long, lngIndex2 As Long 
    Dim vntTemp As Variant, vntBuffer As Variant 
    lngIndex1 = lngLbound 
    lngIndex2 = lngUbound 
    vntBuffer = vntArray((lngLbound + lngUbound) \ 2, intSortColumn) 
    Do 
        If bntSortKey Then 
            Do While vntArray(lngIndex1, intSortColumn) < vntBuffer 
                lngIndex1 = lngIndex1 + 1 
            Loop 
            Do While vntBuffer < vntArray(lngIndex2, intSortColumn) 
                lngIndex2 = lngIndex2 - 1 
            Loop 
        Else 
            Do While vntArray(lngIndex1, intSortColumn) > vntBuffer 
                lngIndex1 = lngIndex1 + 1 
            Loop 
            Do While vntBuffer > vntArray(lngIndex2, intSortColumn) 
                lngIndex2 = lngIndex2 - 1 
            Loop 
        End If 
        If lngIndex1 < lngIndex2 Then 
            If vntArray(lngIndex1, intSortColumn) <> _
                vntArray(lngIndex2, intSortColumn) Then 
                For intIndex = Lbound(vntArray, 2) To Ubound(vntArray, 2) 
                    vntTemp = vntArray(lngIndex1, intIndex) 
                    vntArray(lngIndex1, intIndex) = _
                        vntArray(lngIndex2, intIndex) 
                    vntArray(lngIndex2, intIndex) = vntTemp 
                Next 
            End If 
            lngIndex1 = lngIndex1 + 1 
            lngIndex2 = lngIndex2 - 1 
        ElseIf lngIndex1 = lngIndex2 Then 
            lngIndex1 = lngIndex1 + 1 
            lngIndex2 = lngIndex2 - 1 
        End If 
    Loop Until lngIndex1 > lngIndex2 
    If lngLbound < lngIndex2 Then Call prcQuickSort(lngLbound, _
        lngIndex2, intSortColumn, bntSortKey, vntArray()) 
    If lngIndex1 < lngUbound Then Call prcQuickSort(lngIndex1, _
        lngUbound, intSortColumn, bntSortKey, vntArray()) 
End Sub 
 
 
Gruß Tino
Anzeige
AW: ComboBox schneller sortieren
23.09.2009 12:50:58
Nepumuk
Hi Tino, Rudi,
wieso schreibt ihr den Inhalt der Combobox erst in ein Array und dann wieder zurück? Eine Combobox wie auch eine Listbox ist doch schon ein Array und lässt sich problemlos sortieren.
Gruß
Nepumuk
ja genau, geht auch o.T.
23.09.2009 12:53:28
Tino
AW: ComboBox schneller sortieren
23.09.2009 13:01:13
otto
Hi,
und wie würde das dann aussehen?
otto
@Nepumuk, Nachfrage
23.09.2009 13:09:27
Tino
Hallo Nepumuk,
habe es mal getestet, funktioniert leider nicht.
Wenn ich an prcQuickSort die Combobox.list an byRef vntArray As Variant übergeben wird die Liste in der Combo nicht überschrieben.
Oder meinst Du anders?
Gruß Tino
AW: @Nepumuk, Nachfrage
23.09.2009 13:11:38
Nepumuk
Hi,
ein Beispiel:
' **********************************************************************
' Modul: UserForm3 Typ: Userform
' **********************************************************************

Option Explicit

Private Sub UserForm_Activate()
    Dim intIndex As Integer
    For intIndex = 1 To ThisWorkbook.Sheets.Count
        ListBox1.AddItem Sheets(intIndex).Name
    Next
    Call prcSort(0, ListBox1.ListCount - 1, ListBox1)
End Sub

Private Sub prcSort(lngLBorder As Long, lngUBorder As Long, objControl As Control)
    Dim lngIndex1 As Long, lngIndex2 As Long
    Dim strBuffer As String, strTemp As String
    lngIndex1 = lngLBorder
    lngIndex2 = lngUBorder
    With objControl
        strTemp = UCase$(.List(Fix(lngLBorder + lngUBorder) / 2))
        Do
            Do While UCase$(.List(lngIndex1)) < strTemp
                lngIndex1 = lngIndex1 + 1
            Loop
            Do While strTemp < UCase$(.List(lngIndex2))
                lngIndex2 = lngIndex2 - 1
            Loop
            If lngIndex1 <= lngIndex2 Then
                strBuffer = .List(lngIndex1)
                .List(lngIndex1) = .List(lngIndex2)
                .List(lngIndex2) = strBuffer
                lngIndex1 = lngIndex1 + 1
                lngIndex2 = lngIndex2 - 1
            End If
        Loop Until lngIndex1 > lngIndex2
    End With
    If lngLBorder < lngIndex2 Then Call prcSort(lngLBorder, lngIndex2, objControl)
    If lngIndex1 < lngUBorder Then Call prcSort(lngIndex1, lngUBorder, objControl)
End Sub

Gruß
Nepumuk
Anzeige
@Nepumuk, Nachfrage
23.09.2009 13:27:20
Tino
Hallo,
ach so Du stattest es direkt mit dem Conrol aus.
Kann es aber sein das die Sortierung jetzt länger dauern könnte,
weil ja die Visualisierung auch ständig erneuert wird.
Habe ich jetzt aber nicht getestet.
Gruß Tino
Beispiel
23.09.2009 13:57:12
Rudi
Hallo,
das ist signifikant langsamer als ein Array zu sortieren.
Bei 4000 Zeilen wird's grob. 0,16 Sek. gegen 3,6 Sek.
Gruß
Rudi
AW: Beispiel und Danke an alle
24.09.2009 06:39:58
otto
Hi,
Danke an alle, habe im Netz noch ein schnelleres Beispiel gefunden.
otto

Links zu Excel-Dialogen

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige