Excel 按值对集合排序
我有一组日期,格式为“18011802180317101711等”。 我想能够排序的价值,使最低的是第一,例如17101711180118021803他们 这样做的原因是,我可以按照正确的顺序将它们放入字符串中Excel 按值对集合排序,excel,vba,Excel,Vba,我有一组日期,格式为“18011802180317101711等”。 我想能够排序的价值,使最低的是第一,例如17101711180118021803他们 这样做的原因是,我可以按照正确的顺序将它们放入字符串中 我不知道该怎么做,如果有任何帮助,我将不胜感激。谢谢你这里有一个非常简单的例子: Sub Nathan() Dim c As Collection, arr, brr Dim MyString As String Set c = New Collection
我不知道该怎么做,如果有任何帮助,我将不胜感激。谢谢你这里有一个非常简单的例子:
Sub Nathan()
Dim c As Collection, arr, brr
Dim MyString As String
Set c = New Collection
' Part 1 build Collection
arr = Split("1801,1802,1803,1710,1711", ",")
For Each a In arr
c.Add Val(a)
Next a
' Part 2 put Collection into an array
ReDim brr(1 To c.Count)
For i = 1 To c.Count
brr(i) = c.Item(i)
Next i
' Part 3 sort the array
Call aSort(brr)
' make and output a css
MyString = Join(brr, ",")
MsgBox MyString
End Sub
Public Sub aSort(ByRef InOut)
Dim i As Long, j As Long, Low As Long
Dim Hi As Long, Temp As Variant
Low = LBound(InOut)
Hi = UBound(InOut)
j = (Hi - Low + 1) \ 2
Do While j > 0
For i = Low To Hi - j
If InOut(i) > InOut(i + j) Then
Temp = InOut(i)
InOut(i) = InOut(i + j)
InOut(i + j) = Temp
End If
Next i
For i = Hi - j To Low Step -1
If InOut(i) > InOut(i + j) Then
Temp = InOut(i)
InOut(i) = InOut(i + j)
InOut(i + j) = Temp
End If
Next i
j = j \ 2
Loop
End Sub
考虑使用脚本字典(因为排序已经内置)
编辑#1:
为了避免编写排序
,您可以使用System.Collection.ArrayList
。下面是一个示例,它从列a获取数据,对其进行排序,并将结果放入列B:
Sub AsortedAffair()
Dim arr, o As Object, oc As Long, i As Long
arr = Columns(1).SpecialCells(2)
Set o = CreateObject("System.Collections.ArrayList")
For Each a In arr
o.Add a
Next a
o.Sort
oc = o.Count
For i = 1 To oc
Cells(i, 2) = o.Item(i - 1)
Next i
End Sub
用它构建逗号分隔的字符串也同样容易
请注意,此
项()的索引
属性从零开始,而不是从一开始。您可以在此处找到集合排序功能:可能与的重复。请详细说明脚本字典内置了什么排序功能?@omegastripes My error;排序功能不是内置的。我将编辑我的answer@omegastripes我想到的是System.CollSections.ArrayList
是否有任何解决方案允许在VBA数组/集合和.Net ArrayList之间进行快速的数据传输,而无需循环?@omegastripes据我所知……我只循环了一些键来填充ArrayList,然后在排序后再次循环