Es wäre sehr nett, wenn mir jemand weiterhelfen könnte oder mir erklären könnte worin mein Fehler liegt. Ich will mit meinem Code über eine AktiveX Combobox die direkt im Tabellenblatt eingesetzt ist, verschiedene Benutzerdefinierte Sortierungen auslösen. Jetzt habe ich debug.print am Anfang des Codes eingesetzt, um zu sehen wie oft der Code ausgeführt wird. Wenn ich eine Sortierung in der Combobox auswähle. Es ist leider mehr als 1-mal.
Überall steht das man das "ComboBox1_Change Ereignis mit Application.EnableEvents = False verhindern kann.
Private Sub ComboBox1_DropButtonClick()
Dim wsThis As Worksheet
Set wsThis = ThisWorkbook.Worksheets("Variante1")
Application.EnableEvents = False
Debug.Print "ComboV1_DropButtonClick " & Now
With wsThis.OLEObjects("ComboBox1").Object
.Clear
.List = Array("Sortieren nach", "Datum", "Datum (gefärbt)", "Namen", "Adresse")
End With
Application.EnableEvents = True
End Sub
Private Sub ComboBox1_Change()
Debug.Print "ComboV1_Change " & Now
Dim letzteZ As Long
Dim strKriterium As String
Dim wsThis As Worksheet
Set wsThis = ThisWorkbook.Worksheets("Variante1")
strKriterium = wsThis.OLEObjects("ComboBox1").Object.Value
If strKriterium "Sortieren nach" Or strKriterium = "" Then
letzteZ = wsThis.Cells(wsThis.Rows.Count, 5).End(xlUp).Row 'Zeilenanzahl
With wsThis.Sort
.SortFields.Clear
Select Case strKriterium
Case "Datum", "Datum (gefärbt)"
.SortFields.Add Key:=Range(Cells(5, 7), Cells(letzteZ, 7)), SortOn:=
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 'Datum
.SortFields.Add Key:=Range(Cells(5, 5), Cells(letzteZ, 5)), SortOn:=
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 'Name
.SortFields.Add Key:=Range(Cells(5, 6), Cells(letzteZ, 6)), SortOn:=
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 'Vorname
Case "Namen"
.SortFields.Add Key:=Range(Cells(5, 5), Cells(letzteZ, 5)), SortOn:=
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 'Name
.SortFields.Add Key:=Range(Cells(5, 6), Cells(letzteZ, 6)), SortOn:=
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 'Vorname
.SortFields.Add Key:=Range(Cells(5, 7), Cells(letzteZ, 7)), SortOn:=
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 'Datum
Case "Adresse"
.SortFields.Add Key:=Range(Cells(5, 10), Cells(letzteZ, 10)), SortOn:= _
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 'Adresse
.SortFields.Add Key:=Range(Cells(5, 7), Cells(letzteZ, 7)), SortOn:=
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 'Datum
.SortFields.Add Key:=Range(Cells(5, 5), Cells(letzteZ, 5)), SortOn:=
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 'Name
.SortFields.Add Key:=Range(Cells(5, 6), Cells(letzteZ, 6)), SortOn:=
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 'Vorname
End Select
.SetRange Range(Cells(3, 1), Cells(letzteZ, 25))
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
If strKriterium = "Datum (gefärbt)" Then Call FärbenSpezial(letzteZ)
Application.EnableEvents = False
wsThis.OLEObjects("ComboBox1").Object.Text = "Sortieren nach"
Application.EnableEvents = True
Range("B4").Select
End If
End Sub
Das Funktioniert aber nicht und ich wüsste gerne warum?
Und wüsste vielleicht jemand wie ich es schaffe das der erste Eintrag der auswahlliste in der Auswahl nicht angezeigt wird. Ich will das in der Combobox immer Sortieren nach steht, es aber nach dem aufklappen nicht in der Liste ist.
Danke schonmal und Grüße
Markus