|
楼主 |
发表于 2018-7-30 09:47
|
显示全部楼层
本帖最后由 hugh1236 于 2018-7-30 09:49 编辑
大神非常感谢,不过还得麻烦下你,本来已经达到我设想的排序了,我们领导又加了1个条件,然后我又添加了一个排序(条件3),可是不管我将这段放在哪里都不能满足领导要的排序。现在的要求是“条件”>“条件3”>“条件1”,“条件2”还是降序,“条件1”中同一项中“条件3”按自定义排序(“AAA”,"BBB","CCC"),“条件1”还是“甲”排在后面。就是说“条件”A这类中“条件1”为“甲”的全部放在最后,并按“条件3”排序。其实,“条件1”的数据只有遇到“甲”的时候,才需要将它放在“条件”A类的最后,其它时候“条件1”并不参与排序。因为这个表一直在讨论要达到什么程度,所以我严重怀疑领导可能还要加条件,如果再加我要怎么改?还有就是A列能不能不参与排序,我想让它保持原有的数据(里面为公式),排序后这列变数值了。还请大神帮帮忙,谢谢!
- <div class="blockcode"><blockquote>Option Explicit
- Const TITLENUM = 5 '标题行数量,这里为1行,修改
- Const COL_1 = 6, COL_2 = 13, COL_3 = 5 '"条件1"、"条件2"、"条件"的对应的列号,修改
- Const FINDSTR = "甲" '查找的字符串,修改
- Sub 按钮1_Click()
-
- Application.ScreenUpdating = False
- With Sheet1.Range("B6:o" & Range("A100").End(xlUp).Row)
- '--------------------------------------------------------------------------------------------
- Dim arr, i, j, k, kk, a, b, brr
- arr = Range("a6:o" & Cells(Rows.Count, "d").End(xlUp).Row + 1) '源数据工作表及位置
- brr = arr
- Call dsort(arr, 1, UBound(arr, 1) - 1, COL_3)
- For i = 1 To UBound(arr, 1) - 1
- For j = i To UBound(arr, 1) - 1
- If arr(j, COL_3) <> arr(j + 1, COL_3) Then
- a = i - 1: b = j + 1
- For k = i To j
- If arr(k, COL_1) = FINDSTR Then
- b = b - 1
- For kk = 1 To UBound(arr, 2): brr(b, kk) = arr(k, kk): Next
- Else
- a = a + 1
- For kk = 1 To UBound(arr, 2): brr(a, kk) = arr(k, kk): Next
- End If
- Next
- For k = i To j
- For kk = 1 To UBound(arr, 2): arr(k, kk) = brr(k, kk): Next
- Next
- If a <> i - 1 Then Call dsort(arr, i, a, COL_2)
- If b <> j + 1 Then Call dsort(arr, b, j, COL_2)
- i = j: Exit For
- End If
- Next j, i
- [a6].Resize(UBound(arr, 1), UBound(arr, 2)) = arr
- '---------------------------------------对条件3排序------------------------------------------
- Application.AddCustomList ListArray:=Array("AAA", "BBB", "CCC")
- .Sort Key1:=Range("C6"), Order1:=1, Header:=xlNo, OrderCustom:=Application.CustomListCount + 1
- Application.DeleteCustomList ListNum:=Application.CustomListCount
- '--------------------------------------------------------------------------------------------
- Application.AddCustomList ListArray:=Array("A", "C", "B")
- .Sort Key1:=Range("E6"), Order1:=1, Header:=xlNo, OrderCustom:=Application.CustomListCount + 1
- Application.DeleteCustomList ListNum:=Application.CustomListCount
- End With
-
- Application.ScreenUpdating = True
- End Sub
- Function dsort(arr, first, last, key)
- Dim i, j, k, t
- For i = first To last - 1
- For j = i + 1 To last
- If arr(i, key) < arr(j, key) Then
- For k = 1 To UBound(arr, 2)
- t = arr(i, k): arr(i, k) = arr(j, k): arr(j, k) = t
- Next
- End If
- Next j, i
- End Function
复制代码
|
|