Vba 对字典中的键进行排序
我有一组数字键,我想从最小到最大排序Vba 对字典中的键进行排序,vba,excel,Vba,Excel,我有一组数字键,我想从最小到最大排序 Dim result Set result = CreateObject("Scripting.Dictionary") For i = 1 To N weeksThisIteration = 0 Do While conditiaonTrue //... weeksThisIteration = weeksThisIteration + 1 Loop 'If Not result.Ite
Dim result
Set result = CreateObject("Scripting.Dictionary")
For i = 1 To N
weeksThisIteration = 0
Do While conditiaonTrue
//...
weeksThisIteration = weeksThisIteration + 1
Loop
'If Not result.Item(weeksThisIteration) Then
' result.Add weeksThisIteration, 0//apparently I don't have to initiailize
'End If
result.Item(weeksThisIteration) = result.Item(weeksThisIteration) + 1
我想对结果进行排序
,最好在子/函数中进行排序。我试过:
- 创建子
MySort(作为Scripting.Dictionary列出)
- 调用本机函数
。VBA说func不存在李>SortDictionary
Public Sub SortDictionary(Dict As Scripting.Dictionary, _
SortByKey As Boolean, _
Optional Descending As Boolean = False, _
Optional CompareMode As VbCompareMethod = vbTextCompare)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' SortDictionary
' This sorts a Dictionary object. If SortByKey is False, the
' the sort is done based on the Items of the Dictionary, and
' these items must be simple data types. They may not be
' Object, Arrays, or User-Defined Types. If SortByKey is True,
' the Dictionary is sorted by Key value, and the Items in the
' Dictionary may be Object as well as simple variables.
'
' If sort by key is True, all element of the Dictionary
' must have a non-blank Key value. If Key is vbNullString
' the procedure will terminate.
'
' By defualt, sorting is done in Ascending order. You can
' sort by Descending order by setting the Descending parameter
' to True.
'
' By default, text comparisons are done case-INSENSITIVE (e.g.,
' "a" = "A"). To use case-SENSITIVE comparisons (e.g., "a" <> "A")
' set CompareMode to vbBinaryCompare.
'
' Note: This procedure requires the
' QSortInPlace function, which is described and available for
' download at www.cpearson.com/excel/qsort.htm .
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim Ndx As Long
Dim KeyValue As String
Dim ItemValue As Variant
Dim Arr() As Variant
Dim KeyArr() As String
Dim VTypes() As VbVarType
Dim V As Variant
Dim SplitArr As Variant
Dim TempDict As Scripting.Dictionary
'''''''''''''''''''''''''''''
' Ensure Dict is not Nothing.
'''''''''''''''''''''''''''''
If Dict Is Nothing Then
Exit Sub
End If
''''''''''''''''''''''''''''
' If the number of elements
' in Dict is 0 or 1, no
' sorting is required.
''''''''''''''''''''''''''''
If (Dict.Count = 0) Or (Dict.Count = 1) Then
Exit Sub
End If
''''''''''''''''''''''''''''
' Create a new TempDict.
''''''''''''''''''''''''''''
Set TempDict = New Scripting.Dictionary
If SortByKey = True Then
''''''''''''''''''''''''''''''''''''''''
' We're sorting by key. Redim the Arr
' to the number of elements in the
' Dict object, and load that array
' with the key names.
''''''''''''''''''''''''''''''''''''''''
ReDim Arr(0 To Dict.Count - 1)
For Ndx = 0 To Dict.Count - 1
Arr(Ndx) = Dict.Keys(Ndx)
Next Ndx
''''''''''''''''''''''''''''''''''''''
' Sort the key names.
''''''''''''''''''''''''''''''''''''''
QSortInPlace InputArray:=Arr, LB:=-1, UB:=-1, Descending:=Descending, CompareMode:=CompareMode
''''''''''''''''''''''''''''''''''''''''''''
' Load TempDict. The key value come from
' our sorted array of keys Arr, and the
' Item comes from the original Dict object.
''''''''''''''''''''''''''''''''''''''''''''
For Ndx = 0 To Dict.Count - 1
KeyValue = Arr(Ndx)
TempDict.Add Key:=KeyValue, Item:=Dict.Item(KeyValue)
Next Ndx
'''''''''''''''''''''''''''''''''
' Set the passed in Dict object
' to our TempDict object.
'''''''''''''''''''''''''''''''''
Set Dict = TempDict
''''''''''''''''''''''''''''''''
' This is the end of processing.
''''''''''''''''''''''''''''''''
Else
'''''''''''''''''''''''''''''''''''''''''''''''
' Here, we're sorting by items. The Items must
' be simple data types. They may NOT be Objects,
' arrays, or UserDefineTypes.
' First, ReDim Arr and VTypes to the number
' of elements in the Dict object. Arr will
' hold a string containing
' Item & vbNullChar & Key
' This keeps the association between the
' item and its key.
'''''''''''''''''''''''''''''''''''''''''''''''
ReDim Arr(0 To Dict.Count - 1)
ReDim VTypes(0 To Dict.Count - 1)
For Ndx = 0 To Dict.Count - 1
If (IsObject(Dict.Items(Ndx)) = True) Or _
(IsArray(Dict.Items(Ndx)) = True) Or _
VarType(Dict.Items(Ndx)) = vbUserDefinedType Then
Debug.Print "***** ITEM IN DICTIONARY WAS OBJECT OR ARRAY OR UDT"
Exit Sub
End If
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Here, we create a string containing
' Item & vbNullChar & Key
' This preserves the associate between an item and its
' key. Store the VarType of the Item in the VTypes
' array. We'll use these values later to convert
' back to the proper data type for Item.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Arr(Ndx) = Dict.Items(Ndx) & vbNullChar & Dict.Keys(Ndx)
VTypes(Ndx) = VarType(Dict.Items(Ndx))
Next Ndx
''''''''''''''''''''''''''''''''''
' Sort the array that contains the
' items of the Dictionary along
' with their associated keys
''''''''''''''''''''''''''''''''''
QSortInPlace InputArray:=Arr, LB:=-1, UB:=-1, Descending:=Descending, CompareMode:=vbTextCompare
For Ndx = LBound(Arr) To UBound(Arr)
'''''''''''''''''''''''''''''''''''''
' Loop trhogh the array of sorted
' Items, Split based on vbNullChar
' to get the Key from the element
' of the array Arr.
SplitArr = Split(Arr(Ndx), vbNullChar)
''''''''''''''''''''''''''''''''''''''''''
' It may have been possible that item in
' the dictionary contains a vbNullChar.
' Therefore, use UBound to get the
' key value, which will necessarily
' be the last item of SplitArr.
' Then Redim Preserve SplitArr
' to UBound - 1 to get rid of the
' Key element, and use Join
' to reassemble to original value
' of the Item.
'''''''''''''''''''''''''''''''''''''''''
KeyValue = SplitArr(UBound(SplitArr))
ReDim Preserve SplitArr(LBound(SplitArr) To UBound(SplitArr) - 1)
ItemValue = Join(SplitArr, vbNullChar)
'''''''''''''''''''''''''''''''''''''''
' Join will set ItemValue to a string
' regardless of what the original
' data type was. Test the VTypes(Ndx)
' value to convert ItemValue back to
' the proper data type.
'''''''''''''''''''''''''''''''''''''''
Select Case VTypes(Ndx)
Case vbBoolean
ItemValue = CBool(ItemValue)
Case vbByte
ItemValue = CByte(ItemValue)
Case vbCurrency
ItemValue = CCur(ItemValue)
Case vbDate
ItemValue = CDate(ItemValue)
Case vbDecimal
ItemValue = CDec(ItemValue)
Case vbDouble
ItemValue = CDbl(ItemValue)
Case vbInteger
ItemValue = CInt(ItemValue)
Case vbLong
ItemValue = CLng(ItemValue)
Case vbSingle
ItemValue = CSng(ItemValue)
Case vbString
ItemValue = CStr(ItemValue)
Case Else
ItemValue = ItemValue
End Select
''''''''''''''''''''''''''''''''''''''
' Finally, add the Item and Key to
' our TempDict dictionary.
TempDict.Add Key:=KeyValue, Item:=ItemValue
Next Ndx
End If
'''''''''''''''''''''''''''''''''
' Set the passed in Dict object
' to our TempDict object.
'''''''''''''''''''''''''''''''''
Set Dict = TempDict
End Sub
Public Sub-SortDictionary(Dict As Scripting.Dictionary_
SortByKey作为布尔值_
可选降序为布尔值=False_
可选的CompareMode作为VbCompareMethod=vbTextCompare)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
“SortDictionary
'这将对字典对象进行排序。如果SortByKey为False,则
'根据字典中的项目进行排序,并且
'这些项必须是简单的数据类型。他们可能不是
'对象、数组或用户定义的类型。如果SortByKey是真的,
'字典按键值排序,而
'字典可以是对象,也可以是简单变量。
'
'如果“按键排序”为True,则字典的所有元素
'必须具有非空的键值。如果键是vbNullString
"程序将终止。
'
'通过default,排序是按升序进行的。你可以
'通过设置降序参数按降序排序
”“是真的。
'
'默认情况下,文本比较不区分大小写(例如。,
““a”=“a”)。使用区分大小写的比较(例如,“a”“a”)
'将CompareMode设置为vbBinaryCompare。
'
'注意:此过程需要
'QSortInPlace函数,该函数已描述并可用于
下载地址:www.cpearson.com/excel/qsort.htm。
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
暗Ndx为长
将键值设置为字符串
Dim ItemValue作为变量
Dim Arr()作为变量
Dim KeyArr()作为字符串
将VTypes()设置为VbVarType
Dim V作为变体
Dim SplitArr作为变体
Dim TempDict作为脚本.Dictionary
'''''''''''''''''''''''''''''
“确保Dict不是什么。
'''''''''''''''''''''''''''''
如果Dict什么都不是,那么
出口接头
如果结束
''''''''''''''''''''''''''''
'如果元素的数量
'在Dict中是0或1,否
“需要排序。
''''''''''''''''''''''''''''
如果(Dict.Count=0)或(Dict.Count=1),则
出口接头
如果结束
''''''''''''''''''''''''''''
'创建一个新的临时目录。
''''''''''''''''''''''''''''
Set TempDict=New Scripting.Dictionary
如果SortByKey=True,则
''''''''''''''''''''''''''''''''''''''''
我们正在按键排序。重拨Arr
'中的元素数
'Dict对象,并加载该数组
'使用键名称。
''''''''''''''''''''''''''''''''''''''''
ReDim Arr(0到指令计数-1)
对于Ndx=0,指定计数-1
Arr(Ndx)=指令键(Ndx)
下一个Ndx
''''''''''''''''''''''''''''''''''''''
'对键名称进行排序。
''''''''''''''''''''''''''''''''''''''
QSortInPlace输入阵列:=Arr,LB:=-1,UB:=-1,降序:=降序,比较模式:=CompareMode
''''''''''''''''''''''''''''''''''''''''''''
“加载TempDict。关键值来自
'我们已排序的键数组Arr,以及
'项来自原始Dict对象。
''''''''''''''''''''''''''''''''''''''''''''
对于Ndx=0,指定计数-1
KeyValue=Arr(Ndx)
TempDict.Add Key:=KeyValue,Item:=Dict.Item(KeyValue)
下一个Ndx
'''''''''''''''''''''''''''''''''
'设置传入的Dict对象
“给我们的临时目标。
'''''''''''''''''''''''''''''''''
设置Dict=TempDict
''''''''''''''''''''''''''''''''
'这是处理的结束。
''''''''''''''''''''''''''''''''
其他的
'''''''''''''''''''''''''''''''''''''''''''''''
在这里,我们按项目进行排序。这些项目必须
'必须是简单的数据类型。它们可能不是物体,
'数组或UserDefineType。
'首先,将Arr和VTypes重拨到号码
Dict对象中元素的。阿尔威尔
'保留一个包含
'Item&vbNullChar&Key
“这保持了两者之间的关联
"项目及其关键。
'''''''''''''''''''''''''''''''''''''''''''''''
ReDim Arr(0到指令计数-1)
重拨VTypes(0到指令计数-1)
对于Ndx=0,指定计数-1
如果(IsObject(Dict.Items(Ndx))=真)或_
(IsArray(Dict.Items(Ndx))=真)或_
VarType(Dict.Items(Ndx))=vbUserDefinedType然后
Debug.Print“****字典中的项是对象、数组或UDT”
出口接头
如果结束
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'这里,我们创建一个包含
'Item&vbNullChar&Key
'这将保留项与其属性之间的关联
“钥匙。将项目的VarType存储在VTypes中
'阵列。稍后我们将使用这些值进行转换
'返回到项目的正确数据类型。
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Arr(Ndx)=Dict.Items(Ndx)&vbNullChar&Dict.Keys(Ndx)
VTypes(Ndx)=VarType(Dict.Items(Ndx))
下一个Ndx
''''''''''''''''''''''''''''''''''
'对包含
'词典中的项目
'及其关联的密钥
''''''''''''''''''''''''''''''''''
QSortInPlace输入阵列:=Arr,LB:=-1,UB:=-1,降序:=降序,比较模式:=vbTextCompare
对于Ndx=LBound(Arr)到UBound(Arr)
'''''''''''''''''''''''''''''''''''''
'循环trhogh排序的数组
'项目,基于vbNullChar拆分
'以从元素中获取密钥
数组Arr的。
SplitArr=Split(Arr(Ndx),
' Load TempDict. The key value come from
' our sorted array of keys Arr, and the
' Item comes from the original Dict object.
''''''''''''''''''''''''''''''''''''''''''''
For Ndx = 0 To Dict.Count - 1
KeyValue = Arr(Ndx)
'MsgBox "key: " & KeyValue & "Item Value: " & Dict.Item(CInt(KeyValue))
TempDict.Add Key:=KeyValue, Item:=Dict.Item(CInt(KeyValue))'Convert here
Next Ndx