Re: [算表] vba 篩選排序請教大神哪裡寫錯

作者: windknife18 (windknife18)   2023-09-17 20:23:59
不清楚為什麼要用篩選,如果只是要排序,不需要篩選喔
Sub 篩選和排序()
Dim ws As Worksheet
Dim lastRow As Long
Dim filterRowStart As Long
Dim filterRowEnd As Long
' 定義工作表
Set ws = ThisWorkbook.Sheets("20230918盤前")
' 確定最後一行
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' 找到第一個出現"投本比1日"的上一列
For filterRowStart = 2 To lastRow
If ws.Cells(filterRowStart, 1).Value = "投本比1日" Then
Exit For
End If
Next filterRowStart
' 找到最後一個出現"投本比1日"的列
For filterRowEnd = lastRow To 2 Step -1
If ws.Cells(filterRowEnd, 1).Value = "投本比1日" Then
Exit For
End If
Next filterRowEnd
' 如果找到"投本比1日",則應用AutoFilter
If filterRowStart <= lastRow And filterRowEnd >= filterRowStart Then
' 排序投本比1日%
ws.Rows(filterRowStart & ":" & filterRowEnd).Sort _
key1:=Range("J" & filterRowStart), Order1:=xlDescending, Header:=xlNo
End If
End Sub
作者: mini178 (mini178)   2023-09-20 07:12:00
謝謝大神
作者: windknife18 (windknife18)   2023-09-20 08:51:00
不客氣 :)

Links booklink

Contact Us: admin [ a t ] ucptt.com