Hallo Peter,
danke erstmal für die schnelle Antwort. Aber sorry, was mit Sortierkriterien versus Gültigkeitskriterien an dieser Stelle gemeint ist, hab ich nicht verstanden. Beim Öffnen der UserForm "UFoA" werden routinemäßig verschiedene ComboBoxen und ander Steuerelement mit Daten gefüllt. Unter anderem auch die Listbox "LiBoA". Zuvor werden die Daten, die in die Listbox geladen werden sollen, sortiert. Da beim Arbeiten im gesamten Projekt immer mal wieder Datensätze sortiert werden müssen habe ich eine einzige Sub, die nur sortiert und die ich von jeder Prozedur im Projekt aufrufen kann, konstruiert. Die Sub "Sortieren" läuft problemlos und stabil. Hier mal die Prozedur, die "Sortieren" aufruft ...
Private Sub LiBoA_füllen()
Dim bytSortUpDown As Byte
Dim firstZ As Integer
Dim lastZ As Integer
Dim lngKlientNr As Long
Dim lngOppoNr As Integer
Dim nS As Byte
Dim Produkt As String
Dim S1 As Byte
Dim S2 As Byte
Dim strKlientFolderNam As String
Dim strOppoNam As String
Dim strOppoFileNam As String
UFoA.LiBoA.Clear
n = 0
bytSortUpDown = 11
firstZ = 5
WS = 2
With ThisWorkbook.Sheets(WS)
nS = .UsedRange.Columns.Count
lastZ = .UsedRange.SpecialCells(xlCellTypeLastCell).Row
End With
S1 = 3
S2 = 3
Application.ScreenUpdating = False
Call Sortieren(bytSortUpDown, firstZ, lastZ, nS, S1, S2, WS)
With ThisWorkbook.Sheets(WS)
For Z = 5 To .UsedRange.Rows.Count
If .Cells(Z, 1) = "" Then Exit For
If Mid(.Cells(Z, 1), 1, 2) = bytKlientNr Then
lngOppoNr = .Cells(Z, 1)
Produkt = .Cells(Z, 5)
strOppoNam = .Cells(Z, 2)
strOppoFileNam = .Cells(Z, 3)
With UFoA.LiBoA
.AddItem ""
.List(n, 0) = Z
.List(n, 1) = lngOppoNr
.List(n, 2) = strOppoNam
.List(n, 3) = Produkt
.List(n, 4) = strOppoFileNam
n = n + 1
End With
End If
Next Z
.Activate
.Range("A5").Select
End With
With ThisWorkbook.Sheets(1)
.Activate
.Range("A1").Select
End With
Application.ScreenUpdating = True
End Sub
und hier die hier die Sub, die das eigentliche Sortieren durchführt.
Public Sub Sortieren(bytSortUpDown, firstZ, lastZ, nS, S1, S2, WS)
Dim WksObj As Worksheet
Set WksObj = Worksheets(WS)
If nS = 0 Then nS = WksObj.UsedRange.Columns.Count
If lastZ = 0 Then lastZ = WksObj.Cells(Rows.Count, 1).End(xlUp).Row
With WksObj
.Sort.SortFields.Clear
If Left(bytSortUpDown, 1) = 1 Then .Sort.SortFields.Add Key:=Range(Cells(firstZ, S1), Cells(lastZ, S1)), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
If Left(bytSortUpDown, 1) = 2 Then .Sort.SortFields.Add Key:=Range(Cells(firstZ, S1), Cells(lastZ, S1)), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
If Right(bytSortUpDown, 1) = 1 Then .Sort.SortFields.Add Key:=Range(Cells(firstZ, S2), Cells(lastZ, S2)), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
If Right(bytSortUpDown, 1) = 2 Then .Sort.SortFields.Add Key:=Range(Cells(firstZ, S2), Cells(lastZ, S2)), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With .Sort
.SetRange Range(Cells(firstZ, 1), Cells(lastZ, nS))
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
Set WksObj = Nothing
End Sub
"Sortieren" setze ich auch bei andern Projekten ein und überall läuft diese Sub problemlos. Ich vermute, dass sich da zwei Vorgänge irgendwie gegenseitig stören. Nur bin ich noch nicht dahinter gestiegen was genau. Manchmal ist man ja auch Betriebsblind und ein Außenstehender sieht den Fehler sofort. Das jedenfalls ist meine Hoffnung in dieser Sache.
Liebe Grüße Rainer.