VBA技术求助:将S1:S6数据移至T1并去重降序排序
Hey John, nice work getting the filtering and data migration part working! Let’s fix that missing sorting step for you.
Assuming you’re using Excel VBA for this task, here’s how to add the descending sort right after your existing filtering/migration code:
Step 1: Add Code to Define the Target Range
First, we need to find the last row with data in column T (since your filtered data starts at T1). Then we can sort that entire range.
Add this snippet after your existing Filter Result code:
' Find the last row with data in column T Dim lastRow As Long lastRow = Range("T" & Rows.Count).End(xlUp).Row ' Sort the target range (T1 to T[lastRow]) in descending order Range("T1:T" & lastRow).Sort _ Key1:=Range("T1"), _ Order1:=xlDescending, _ Header:=xlNo ' Change to xlYes if T1 is a header row, not data
Quick Explanation:
lastRowcalculates the bottom-most row with data in column T, so we don’t accidentally sort empty cells.- The
Sortmethod targets your filtered data range:Key1:=Range("T1")tells Excel to sort based on values in column T.Order1:=xlDescendingsets the sort order from highest to lowest.Headeradjusts whether Excel treats the first row as a header (usexlYesif T1 is a label, not a number; keepxlNoif T1 is your first data point).
If you were using Excel formulas instead of VBA, you could even combine the deduplication and sorting in one step with:
=SORT(UNIQUE(S1:S6), , -1)
Just paste that into cell T1, and it’ll automatically output the sorted, unique values from S1:S6.
Hope this gets you sorted out (pun intended)! 😊
内容的提问来源于stack exchange,提问作者JonhDoe

