Sub NachSpalteASortieren3()
Const cPASSWORT =
Const cERSTEZEILE = 2
Const cSP_A = 1
Const cSP_B = 2
Const cSP_C = 3
Dim ws As Worksheet, Zelle As Range
Dim lRowLast As Long, lRow As Long, x As Long
Dim sErtseAdresse As String
Dim bLeerZeile As Boolean
Dim lVonSP As Long, lBisSP As Long
Set ws = ActiveSheet
->zu sortierenden Bereich feststellen
->---SPALTEN
lVonSP = 1
lBisSP = ws.UsedRange.Columns.Count - 1 + ws.UsedRange.Column
->---ZEILEN
lRowLast = 0
For x = lVonSP To lBisSP
lRow = ws.Cells(ws.Rows.Count, x).End(xlUp).Row
If lRowLast < lRow Then lRowLast = lRow
Next
->nur sortieren, wenn mehr als eine Zeile vorhanden ist
If lRowLast > cERSTEZEILE Then
->sortieren
ws.Range(ws.Cells(cERSTEZEILE, lVonSP), ws.Cells(lRowLast, lBisSP)).Sort _
Key1:=ws.Cells(cERSTEZEILE, cSP_A), Order1:=xlAscending, _
Key2:=ws.Cells(cERSTEZEILE, cSP_B), Order2:=xlAscending, _
Key3:=ws.Cells(cERSTEZEILE, cSP_C), Order3:=xlAscending, _
Header:=xlNo
->erste leer Zeile im (Sortierbereich plus eine Zeile) finden,
->und Zelle in Spalte A markieren
Set Zelle = ws.Range(ws.Cells(cERSTEZEILE, cSP_A), ws.Cells(lRowLast + 1, cSP_A)).Find( _
What:=, _
After:=ws.Cells(cERSTEZEILE, cSP_A), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False, _
SearchFormat:=False)
If Zelle Is Nothing Then
MsgBox ( _
Fehler in NachSpalteASortieren2. & vbLf & _
Bitte rufen Sie ihren Arzt oder Administrator.)
GoTo AUFRAEUMEN
End If
->letzte Zeile plus 1 ist Ausgangspunkt
sErtseAdresse = Zelle.Address
lRow = Zelle.Row
->gibt es oberhalb noch leere Zeilen
Do
Set Zelle = ws.Range(ws.Cells(cERSTEZEILE, cSP_A), _
ws.Cells(lRowLast + 1, cSP_A)).FindPrevious(After:=Zelle)
If Zelle.Address = sErtseAdresse Then Exit Do
bLeerZeile = True
For x = lVonSP To lBisSP
If ws.Cells(Zelle.Row, x).Value <> Then
bLeerZeile = False
End If
Next
If bLeerZeile Then lRow = Zelle.Row
Loop
ws.Range(ws.Cells(lRow, cSP_A), ws.Cells(lRow, cSP_A)).Activate
Else
ws.Range(ws.Cells(lRowLast + 1, cSP_A), ws.Cells(lRowLast + 1, cSP_A)).Activate
End If
AUFRAEUMEN:
Set ws = Nothing: Set Zelle = Nothing
End Sub