按一列排序,然后在第二列中对相同的值进行分组?



我的电子表格中有 2 列,序列号和日期。我已经将日期列按升序排序,结果如下所示:

Serial Number     Date
22222             02/09/2020
11111             02/10/2020
33333             02/11/2020
11111             02/12/2020
22222             02/13/2020
44444             02/14/2020

我想使用 2016 VBA 从这里开始做的是将所有相同的序列号组合在一起,将每个序列号的最快日期作为第一个序列号,如下所示:

Serial Number     Date
22222             02/09/2020
22222             02/13/2020
11111             02/10/2020
11111             02/12/2020
33333             02/11/2020
44444             02/14/2020

因此,我最终想要的是首先列出最快日期的电子表格,但是如果有多个序列号相同,则将它们调上列表并按序列号分组,但保持日期升序,以及其他序列号。

我希望有比 for 循环更好的东西,但我的伪代码想法是这样的:

For Each cel in Range(Serial Number column)
If vLookup cel in Range(Serial Number column) = True Then
Get Row Number of the Match, Cut entire row, and Insert it below cels row (ie. cel.offset(1,0))
Else
Do Nothing
End If
Next cel

这将在新插入的序列号上重复该过程,以查找另一个匹配项,以防列表下方有另一个匹配项,它会捕获它。如果有人能帮我编程这个东西,或者如果他们有更好的主意,我全都听着。

谢谢!今晚晚些时候将进行审查。

请使用下一个代码,请:

Sub SortTwoColumns()
Dim sh As Worksheet
Set sh = ActiveSheet 'use here your worksheet
sh.Columns("A:B").Sort key1:=sh.Range("A1"), _
order1:=xlAscending, Key2:=sh.Range("B1"), order2:=xlAscending, Header:=xlYes
End Sub

最新更新