Add SortDataRows common function
This commit is contained in:
@@ -131,3 +131,40 @@ Function ReadCSVFile(ByVal filePath As String, Optional ByVal charset As String
|
||||
|
||||
ReadCSVFile = Split(textContent, vbLf)
|
||||
End Function
|
||||
|
||||
Sub SortDataRows(Optional ByVal sortColumn As Long = 3)
|
||||
Dim ws As Worksheet
|
||||
Dim lastRow As Long
|
||||
Dim startRow As Long
|
||||
Dim sortOrder As Long
|
||||
|
||||
Set ws = ActiveSheet
|
||||
startRow = 7
|
||||
lastRow = GetLastDataRow(ws, sortColumn)
|
||||
|
||||
If lastRow < startRow Then
|
||||
MsgBox "No data to sort.", vbExclamation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' Determine sort order based on first row's current state
|
||||
Dim currentFirst As String
|
||||
Dim nextFirst As String
|
||||
currentFirst = Trim(ws.Cells(startRow, sortColumn).Value)
|
||||
nextFirst = Trim(ws.Cells(startRow + 1, sortColumn).Value)
|
||||
|
||||
If currentFirst <> "" And nextFirst <> "" Then
|
||||
If currentFirst > nextFirst Then
|
||||
sortOrder = xlAscending
|
||||
Else
|
||||
sortOrder = xlDescending
|
||||
End If
|
||||
Else
|
||||
sortOrder = xlAscending
|
||||
End If
|
||||
|
||||
ws.Range(ws.Cells(startRow, 1), ws.Cells(lastRow, 20)).Sort _
|
||||
Key1:=ws.Cells(startRow, sortColumn), _
|
||||
Order1:=sortOrder, _
|
||||
Header:=xlNo
|
||||
End Sub
|
||||
|
||||
Reference in New Issue
Block a user