我正在尋找有關最終用戶點擊按鈕并對資料進行排序的代碼的幫助,但將“組”保持在一起(從 A 列到 AA 列)以及任務之間的空間。
我在網上做了一些研究,但沒有任何東西可以作業,所以我什至沒有一個基本代碼可以開始。
這里有一些圖片來展示我正在努力完成的事情。
第一張圖片顯示了可能已經輸入的任務,但是我們在輸入所有任務后分配優先級,如您所見,它們是亂序的。

然后我希望他們點擊影像左上角的“排序”按鈕,它根據優先級對作業表進行排序,1 是第一個任務,向下到最后一個任務,但保留“組” " 和任務之間的空間,所以它最終看起來像這樣:

Again, columns affected would be from A through AA (i.e., the data needing to stay together spans between those columns).
I don't know if this is even possible, but any help would be GREATLY appreciated.
EDIT:
I created a thread on ExcelForum so I can post an actual spreadsheet... that post is here: https://www.excelforum.com/excel-programming-vba-macros/1362269-sort-groups-and-keep-spaces.html#post5585545
uj5u.com熱心網友回復:
使用 2 個備用列來保存排序順序鍵。
Option Explicit
Sub sortit()
Dim wb As Workbook, ws As Worksheet
Dim LastRow As Long, r As Long
Dim p As Integer, n As Long
Set wb = ThisWorkbook
Set ws = wb.Sheets(1)
With ws
LastRow = .UsedRange.Rows.Count .UsedRange.Row - 1
' use column AB,AC for sort order
For r = 4 To LastRow
If .Cells(r, "A") > 0 Then
p = .Cells(r, "A")
n = 0
End If
n = n 1
.Cells(r, "AB") = p
.Cells(r, "AC") = n
Next
With .Sort
.SortFields.Clear
.SortFields.Add Key:=ws.Range("AB4"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=ws.Range("AC4"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.SetRange ws.Range("A4:AC" & LastRow)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' clear columns
.Columns("AB:AC").Clear
End With
MsgBox "Sorted"
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/331469.html
上一篇:Excel中的字串總和
下一篇:基于當前活動行的動態選擇更改
