Live-Forum - Die aktuellen Beiträge
Datum
Titel
28.03.2024 21:12:36
28.03.2024 18:31:49
Anzeige
Archiv - Navigation
1492to1496
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 sortieren

ComboBox sortieren
19.05.2016 08:13:34
Marco
Morgen alle zam, ich würde gerne wissen wie man eine ComboBox sortiert. Aktuell muss ich erst einen Button dürcken der die Tabelle sortiert und dann verwende ich die ComboBox. Ich würde aber gerne gleich die ComboBox aufmachen ohne vorher einen Knopf zu drücken. Geht denn das?
Mein Code sieht aktuelle so aus:
Sub Makro2()
Range("P9:S26").Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("Analysis Statistic").AutoFilter.Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Analysis Statistic").AutoFilter.Sort.SortFields.Add _
Key:=Range("P9:P26"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Analysis Statistic").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Diesen kann ich aber nicht einfach zu dem Code von der ComboBox hinzufügen oder?
Grüße Marco

10
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: ComboBox sortieren
19.05.2016 08:20:16
Nepumuk
Hallo,
der Code mit dem du die Combobox füllst ist entscheidend, denn wie ich eine Tabelle sortiere ist mir bekannt.
Gruß
Nepumuk

AW: ComboBox sortieren
19.05.2016 11:06:54
Marco
Ach so das wusste ich nicht Nepumuk. Also meine Combobox wird folgendermaßen gefüllt:
Option Explicit
'Modulweite Variablen deklarieren.
Const C_mstrDatenblatt As String = "Database"
Const C_mstrZielblatt As String = "Dashboard"
Dim mobjDic As Object
Dim mlngLast As Long
Dim mlngZ As Long
Private Sub ComboBox1_Enter()
'Erste Combobox. Jeder Standort aus Spalte A wird nur einmal angezeigt.
Set mobjDic = CreateObject("Scripting.Dictionary")
With Worksheets(C_mstrDatenblatt)
For mlngZ = 5 To mlngLast
If Not IsEmpty(.Cells(mlngZ, 1).Value) Then _
mobjDic(.Cells(mlngZ, 1).Value) = 0
Next
End With
Me.ComboBox1.List = mobjDic.keys
Set mobjDic = Nothing
End Sub
Also kann ich die Sortierung nicht einfach aufzeichnen und einfügen?
Gruß Marco

Anzeige
AW: ComboBox sortieren
19.05.2016 12:17:57
Nepumuk
Hallo,
warum machst du das im Enter-Event der Combobox? Die Liste ändert sich doch nicht zur Laufzeit des Userforms.
Private Sub UserForm_Activate()
    'Erste Combobox. Jeder Standort aus Spalte A wird nur einmal angezeigt.
    
    Set mobjDic = CreateObject("Scripting.Dictionary")
    
    With Worksheets(C_mstrDatenblatt)
        For mlngZ = 5 To mlngLast
            If Not IsEmpty(.Cells(mlngZ, 1).Value) Then _
                mobjDic(.Cells(mlngZ, 1).Value) = 0
        Next
    End With
    
    ComboBox1.List = mobjDic.keys
    Set mobjDic = Nothing
    
    Call QickSort(0, ComboBox1.ListCount - 1)
    
End Sub

Private Sub QickSort(ByVal pvlngLBorder As Long, ByVal pvlngUBorder As Long)
    
    Dim ialngIndex1 As Long, ialngIndex2 As Long
    Dim strBuffer As String, strTemp As String
    
    ialngIndex1 = pvlngLBorder
    ialngIndex2 = pvlngUBorder
    
    With ComboBox1
        strTemp = .List((ialngIndex1 + ialngIndex2) \ 2, 0)
        Do
            Do While .List(ialngIndex1, 0) < strTemp
                ialngIndex1 = ialngIndex1 + 1
            Loop
            Do While strTemp < .List(ialngIndex2, 0)
                ialngIndex2 = ialngIndex2 - 1
            Loop
            If ialngIndex1 <= ialngIndex2 Then
                strBuffer = .List(ialngIndex1, 0)
                .List(ialngIndex1, 0) = _
                    .List(ialngIndex2, 0)
                .List(ialngIndex2, 0) = strBuffer
                ialngIndex1 = ialngIndex1 + 1
                ialngIndex2 = ialngIndex2 - 1
            End If
        Loop Until ialngIndex1 > ialngIndex2
    End With
    If pvlngLBorder < ialngIndex2 Then Call QickSort(pvlngLBorder, ialngIndex2)
    If ialngIndex1 < pvlngUBorder Then Call QickSort(ialngIndex1, pvlngUBorder)
End Sub

Gruß
Nepumuk

Anzeige
AW: ComboBox sortieren
19.05.2016 14:16:03
Marco
Also erst mal danke Nepumuk für deine Hilfe. Und jetzt zu deiner Frage. Ich habe die ComboBox erstellt und dann Codeanzeigen geklickt und dann Stand schon Combobox_Enter() da. Ich wusste nicht dass das Enter eine besondere Bedeutung hat. Ich war der Auffassung das es nur eine Beschrift von der ComboBox ist mehr nicht.
Gruß Marco

AW: ComboBox sortieren
19.05.2016 14:34:12
Marco
Nepumuk ist es irgendwie relevant das es:
Private Sub UserForm_Activate()
heißt anstatt?
Private Sub ComboBox1_Enter()
Ich habe nämlich zwei ComboBoxen und die zweite hängt von der ersten ab. Ich habe jetzt deinen Qicksort genommen ihn für die erste ComboBox hergenommen und ihn für die zweite nur mit anderen Variablen eingesetzt. Es funktioniert auch. Ist das aber jetzt nur Zufall, Glück oder richtig so? Der komplette Code sieht jetzt so aus:
Option Explicit
'Modulweite Variablen deklarieren.
Const C_mstrDatenblatt As String = "Database"
Const C_mstrZielblatt As String = "Dashboard"
Dim mobjDic As Object
Dim mlngLast As Long
Dim mlngZ As Long
Private Sub UserForm_Activate()
'Erste Combobox. Jeder Standort aus Spalte A wird nur einmal angezeigt.
Set mobjDic = CreateObject("Scripting.Dictionary")
With Worksheets(C_mstrDatenblatt)
For mlngZ = 5 To mlngLast
If Not IsEmpty(.Cells(mlngZ, 1).Value) Then _
mobjDic(.Cells(mlngZ, 1).Value) = 0
Next
End With
ComboBox1.List = mobjDic.keys
Set mobjDic = Nothing
Call QickSort1(0, ComboBox1.ListCount - 1)
End Sub
Private Sub QickSort1(ByVal pvlngLBorder1 As Long, ByVal pvlngUBorder1 As Long)
Dim ialngIndex1 As Long, ialngIndex2 As Long
Dim strBuffer1 As String, strTemp1 As String
ialngIndex1 = pvlngLBorder1
ialngIndex2 = pvlngUBorder1
With ComboBox1
strTemp1 = .List((ialngIndex1 + ialngIndex2) \ 2, 0)
Do
Do While .List(ialngIndex1, 0)  ialngIndex2
End With
If pvlngLBorder1  ialngIndex4
End With
If pvlngLBorder2 = 0 And ComboBox2.ListIndex >= 0 Then
With Worksheets(C_mstrDatenblatt)
For iZeile = 5 To .Cells(.Rows.Count, 1).End(xlUp).Row
If .Cells(iZeile, 1) = ComboBox1 And .Cells(iZeile, 2) = ComboBox2 Then
zeile = iZeile
Exit For
End If
Next iZeile
End With
Unload Me
UserForm2.Show
End If
End Sub
Private Sub CommandButton3_Click()
'UserForm2 öffnen.
Unload Me
UserForm3.Show
End Sub
Private Sub UserForm1_Schließen_Click()
'UserForm1 schließen.
Unload Me
End Sub
Private Sub UserForm_Initialize()
'Bei Start des Userform1 wird die unterste Zeile in Spalte A ermittelt.
mlngLast = Worksheets(C_mstrDatenblatt).Cells(Rows.Count, 1).End(xlUp).Row
End Sub

Anzeige
AW: ComboBox sortieren
19.05.2016 16:11:37
Nepumuk
Hallo,
könnte man "schöner" machen, aber wichtig ist dass es funktioniert.
Gruß
Nepumuk

AW: ComboBox sortieren
19.05.2016 16:29:22
Marco
Echt schöner? Du meinst schlanker und so? Wie denn oh großer Nepumuk

AW: ComboBox sortieren
19.05.2016 16:59:46
Nepumuk
Hallo,
so:
Option Explicit

Const C_mstrDatenblatt As String = "Database"
Const C_mstrZielblatt As String = "Dashboard"

Dim mlngLast As Long

Private Sub UserForm_Activate()
    
    Dim mobjDic As Object
    Dim mlngZ As Long
    
    'Erste Combobox. Jeder Standort aus Spalte A wird nur einmal angezeigt.
    
    Set mobjDic = CreateObject("Scripting.Dictionary")
    
    With Worksheets(C_mstrDatenblatt)
        For mlngZ = 5 To mlngLast
            If Not IsEmpty(.Cells(mlngZ, 1).Value) Then _
                mobjDic(.Cells(mlngZ, 1).Value) = 0
        Next
    End With
    
    ComboBox1.List = mobjDic.keys
    
    Call QickSort(0, ComboBox1.ListCount - 1, ComboBox1)
    
    Call mobjDic.RemoveAll
    
    With Worksheets(C_mstrDatenblatt)
        For mlngZ = 5 To mlngLast
            If .Cells(mlngZ, 1).Value = Me.ComboBox1.Value Then _
                mobjDic(.Cells(mlngZ, 2).Value) = 0
        Next
    End With
    
    ComboBox2.List = mobjDic.keys
    
    Call QickSort(0, ComboBox2.ListCount - 1, ComboBox2)
    
    Set mobjDic = Nothing
    
End Sub

Private Sub QickSort(ByVal pvlngLBorder1 As Long, _
        ByVal pvlngUBorder1 As Long, ByRef probjCombobox As MSForms.ComboBox)

    
    Dim ialngIndex1 As Long, ialngIndex2 As Long
    Dim strBuffer1 As String, strTemp1 As String
    
    ialngIndex1 = pvlngLBorder1
    ialngIndex2 = pvlngUBorder1
    
    With probjCombobox
        strTemp1 = .List((ialngIndex1 + ialngIndex2) \ 2, 0)
        Do
            Do While .List(ialngIndex1, 0) < strTemp1
                ialngIndex1 = ialngIndex1 + 1
            Loop
            Do While strTemp1 < .List(ialngIndex2, 0)
                ialngIndex2 = ialngIndex2 - 1
            Loop
            If ialngIndex1 <= ialngIndex2 Then
                strBuffer1 = .List(ialngIndex1, 0)
                .List(ialngIndex1, 0) = _
                    .List(ialngIndex2, 0)
                .List(ialngIndex2, 0) = strBuffer1
                ialngIndex1 = ialngIndex1 + 1
                ialngIndex2 = ialngIndex2 - 1
            End If
        Loop Until ialngIndex1 > ialngIndex2
    End With
    If pvlngLBorder1 < ialngIndex2 Then Call QickSort(pvlngLBorder1, ialngIndex2, probjCombobox)
    If ialngIndex1 < pvlngUBorder1 Then Call QickSort(ialngIndex1, pvlngUBorder1, probjCombobox)
End Sub

Private Sub CommandButton1_Click()
    'Bestätigt die ausgewählten Combobox Werte, schließt das UserForm1 und öffnet das UserForm2.
    
    Dim iZeile As Long
    If ComboBox1.ListIndex >= 0 And ComboBox2.ListIndex >= 0 Then
        With Worksheets(C_mstrDatenblatt)
            For iZeile = 5 To .Cells(.Rows.Count, 1).End(xlUp).Row
                If .Cells(iZeile, 1) = ComboBox1 And .Cells(iZeile, 2) = ComboBox2 Then
                    zeile = iZeile
                    Exit For
                End If
            Next iZeile
        End With
        
        Unload Me
        UserForm2.Show
    End If
End Sub

Private Sub CommandButton3_Click()
    'UserForm2 öffnen.
    
    Unload Me
    UserForm3.Show
End Sub

Private Sub UserForm1_Schließen_Click()
    'UserForm1 schließen.
    
    Unload Me
End Sub

Private Sub UserForm_Initialize()
    'Bei Start des Userform1 wird die unterste Zeile in Spalte A ermittelt.
    
    mlngLast = Worksheets(C_mstrDatenblatt).Cells(Rows.Count, 1).End(xlUp).Row
End Sub

Gruß
Nepumuk

Anzeige
Nachtrag
19.05.2016 18:27:22
Nepumuk
Hallo,
ich hab gerade entdeckt das die Combobox2 von der ersten abhängig ist. Daher:
Option Explicit

Const C_mstrDatenblatt As String = "Database"
Const C_mstrZielblatt As String = "Dashboard"

Dim mlngLast As Long

Private Sub ComboBox1_Change()
    
    Dim mobjDic As Object
    Dim mlngZ As Long
    
    Set mobjDic = CreateObject("Scripting.Dictionary")
    
    With Worksheets(C_mstrDatenblatt)
        For mlngZ = 5 To mlngLast
            If .Cells(mlngZ, 1).Value = ComboBox1.Value Then _
                mobjDic(.Cells(mlngZ, 2).Value) = 0
        Next
    End With
    
    ComboBox2.List = mobjDic.keys
    
    Call QickSort(0, ComboBox2.ListCount - 1, ComboBox2)
    
    Set mobjDic = Nothing
    
End Sub

Private Sub UserForm_Activate()
    
    Dim mobjDic As Object
    Dim mlngZ As Long
    
    'Erste Combobox. Jeder Standort aus Spalte A wird nur einmal angezeigt.
    
    Set mobjDic = CreateObject("Scripting.Dictionary")
    
    With Worksheets(C_mstrDatenblatt)
        For mlngZ = 5 To mlngLast
            If Not IsEmpty(.Cells(mlngZ, 1).Value) Then _
                mobjDic(.Cells(mlngZ, 1).Value) = 0
        Next
    End With
    
    ComboBox1.List = mobjDic.keys
    
    Call QickSort(0, ComboBox1.ListCount - 1, ComboBox1)
    
    Set mobjDic = Nothing
    
End Sub

Private Sub QickSort(ByVal pvlngLBorder1 As Long, _
        ByVal pvlngUBorder1 As Long, ByRef probjCombobox As MSForms.ComboBox)

    
    Dim ialngIndex1 As Long, ialngIndex2 As Long
    Dim strBuffer1 As String, strTemp1 As String
    
    ialngIndex1 = pvlngLBorder1
    ialngIndex2 = pvlngUBorder1
    
    With probjCombobox
        strTemp1 = .List((ialngIndex1 + ialngIndex2) \ 2, 0)
        Do
            Do While .List(ialngIndex1, 0) < strTemp1
                ialngIndex1 = ialngIndex1 + 1
            Loop
            Do While strTemp1 < .List(ialngIndex2, 0)
                ialngIndex2 = ialngIndex2 - 1
            Loop
            If ialngIndex1 <= ialngIndex2 Then
                strBuffer1 = .List(ialngIndex1, 0)
                .List(ialngIndex1, 0) = _
                    .List(ialngIndex2, 0)
                .List(ialngIndex2, 0) = strBuffer1
                ialngIndex1 = ialngIndex1 + 1
                ialngIndex2 = ialngIndex2 - 1
            End If
        Loop Until ialngIndex1 > ialngIndex2
    End With
    If pvlngLBorder1 < ialngIndex2 Then Call QickSort(pvlngLBorder1, ialngIndex2, probjCombobox)
    If ialngIndex1 < pvlngUBorder1 Then Call QickSort(ialngIndex1, pvlngUBorder1, probjCombobox)
End Sub

Private Sub CommandButton1_Click()
    'Bestätigt die ausgewählten Combobox Werte, schließt das UserForm1 und öffnet das UserForm2.
    
    Dim iZeile As Long
    If ComboBox1.ListIndex >= 0 And ComboBox2.ListIndex >= 0 Then
        With Worksheets(C_mstrDatenblatt)
            For iZeile = 5 To .Cells(.Rows.Count, 1).End(xlUp).Row
                If .Cells(iZeile, 1) = ComboBox1 And .Cells(iZeile, 2) = ComboBox2 Then
                    zeile = iZeile
                    Exit For
                End If
            Next iZeile
        End With
        
        Unload Me
        UserForm2.Show
    End If
End Sub

Private Sub CommandButton3_Click()
    'UserForm2 öffnen.
    
    Unload Me
    UserForm3.Show
End Sub

Private Sub UserForm1_Schließen_Click()
    'UserForm1 schließen.
    
    Unload Me
End Sub

Private Sub UserForm_Initialize()
    'Bei Start des Userform1 wird die unterste Zeile in Spalte A ermittelt.
    
    mlngLast = Worksheets(C_mstrDatenblatt).Cells(Rows.Count, 1).End(xlUp).Row
End Sub

Gruß
Nepumuk

Anzeige
AW: Nachtrag
19.05.2016 20:00:16
Marco
Danke für alles Nepumuk und danke für deinen Nachtrag. Ich konnte erst jetzt ausprobieren ich hatte bis um 6 Schule. Es funktioniert fehlerfrei danke danke =)

Links zu Excel-Dialogen

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige