AW: Sortieren mit VBA
Harald
Hallo Katrin,
Schritt für Schritt kommst Du zum Ziel:
Sub test()
Dim lastrow As Long
Dim i As Long
lastrow = Worksheets("Tabelle1").Range("a65536").End(xlUp).Row 'Setzt voraus, dass alle Einträge eine Kundennummer haben
'Teil 1: Zellen ohne "j" erhalten ein "n"
For i = 2 To lastrow
If UCase(Cells(i, 3)) <> "J" Then 'Ucase, damit Groß-/Kleinschreibung egal ist
Cells(i, 3) = "n"
End If
Next i
'Teil 2: Sortieren nach KZ
Columns("A:D").Select
Selection.Sort Key1:=Range("C2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
'Teil 3: Sortieren nach Kundennummer in dem Bereich KZ="n"
For i = 2 To lastrow
If UCase(Cells(i, 3)) = "N" Then
Exit For ' Finde erste Zeile mit KZ=N
End If
Next i
Range("A" & i & ":D" & lastrow).Select
Selection.Sort Key1:=Range("A" & i), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
'Teil 3: Sortieren nach Verbrauch
For i = 2 To lastrow
If UCase(Cells(i, 3)) = "N" And Cells(i, 1) > 5000 Then
Exit For ' Finde erste Zeile mit KZ=N und Kundennummer > 5000
End If
Next i
Range("A" & i & ":D" & lastrow).Select
Selection.Sort Key1:=Range("D" & i), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End Sub
Gruß Harald