excel - 对组进行排序并保留空格
问题描述
我正在寻找有关最终用户单击按钮并对数据进行排序的代码的帮助,但将“组”保持在一起(从 A 列到 AA 列)以及任务之间的空格。
我在网上做了一些研究,但没有得到任何工作,所以我什至没有基本代码可以开始。
这里有一些图片来展示我想要完成的事情。
第一张图片显示了可能已经输入的任务,但是我们在输入所有任务后分配优先级,如您所见,它们是无序的。
然后我想让他们点击图像左上角的“排序”按钮,它会根据优先级对工作表进行排序,1 是第一个任务,一直到最后一个,但保留“组” " 以及任务之间的空间,所以它最终看起来像这样:
同样,受影响的列将从 A 到 AA(即,需要保持在一起的数据跨越这些列)。
我不知道这是否可能,但任何帮助将不胜感激。
编辑:
我在 ExcelForum 上创建了一个线程,这样我就可以发布一个实际的电子表格......该帖子在这里:https ://www.excelforum.com/excel-programming-vba-macros/1362269-sort-groups-and-keep-spaces .html#post5585545
解决方案
使用 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
推荐阅读
- matlab - 如何在 ODE 求解器中使用符号表达式
- c# - 论点 1:无法从 'int[][]' 转换为 'int[*,*]'
- microsoft-graph-api - 无法找到employeeId 的值
- python - 如何在没有 python oauth2client lib 的情况下实现谷歌登录混合服务器端流程
- c - 输入带有条件的数组
- angularjs - Highcharts 饼图系列颜色不变
- python - 通过 pip 安装 pandas 会引发错误
- oauth-2.0 - 在没有 client_secret 的情况下检索 oauth 访问令牌
- php - 如何在 woocommerce 上为特殊产品申请运费折扣?
- go - 使用现有的 SSH 连接连接到远程主机