Code: Select all
....
Dim MYDIC As Object
Set MYDIC = New Scripting.Dictionary
....
If Not MYDIC.Exists(COD) Then
MYDIC.Add COD, DESCR_COD
End If
note:
COD and DESCR_COD are string var
Code: Select all
....
Dim MYDIC As Object
Set MYDIC = New Scripting.Dictionary
....
If Not MYDIC.Exists(COD) Then
MYDIC.Add COD, DESCR_COD
End If
Code: Select all
Sub SortDict(dic As Scripting.Dictionary)
Dim i As Long
Dim j As Long
Dim tmp As String
Dim arr() As String
Dim tmpdic As Scripting.Dictionary
' Get out if dictionary is empty
If dic Is Nothing Then
Exit Sub
End If
' or if it has at most one item
If dic.Count <= 1 Then
Exit Sub
End If
' Fill arrays with arr
ReDim arr(0 To dic.Count - 1)
For i = 0 To dic.Count - 1
arr(i) = dic.Keys(i)
Next i
' Sort the array
For i = LBound(arr) To UBound(arr) - 1
For j = i + 1 To UBound(arr)
If arr(i) > arr(j) Then
tmp = arr(i)
arr(i) = arr(j)
arr(j) = tmp
End If
Next j
Next i
' Fill new dictionary sorted by key
Set tmpdic = New Scripting.Dictionary
For i = 0 To dic.Count - 1
tmpdic.Add Key:=arr(i), Item:=dic.Item(arr(i))
Next i
' Set original dictionary to temp one
Set dic = tmpdic
End Sub
NOTE:HansV wrote:Add the following procedure:
To sort your dictionary by key, callCode: Select all
Sub SortDict(dic As Scripting.Dictionary) Dim i As Long Dim j As Long Dim tmp As String Dim arr() As String Dim tmpdic As Scripting.Dictionary ' Get out if dictionary is empty If dic Is Nothing Then Exit Sub End If ' or if it has at most one item If dic.Count <= 1 Then Exit Sub End If ' Fill arrays with arr ReDim arr(0 To dic.Count - 1) For i = 0 To dic.Count - 1 arr(i) = dic.Keys(i) Next i ' Sort the array For i = LBound(arr) To UBound(arr) - 1 For j = i + 1 To UBound(arr) If arr(i) > arr(j) Then tmp = arr(i) arr(i) = arr(j) arr(j) = tmp End If Next j Next i ' Fill new dictionary sorted by key Set tmpdic = New Scripting.Dictionary For i = 0 To dic.Count - 1 tmpdic.Add Key:=arr(i), Item:=dic.Item(arr(i)) Next i ' Set original dictionary to temp one Set dic = tmpdic End Sub
SortDict MYDIC
Code: Select all
Dim tmpdic As Scripting.Dictionary
Good!HansV wrote:The line
declares tmpdic as a Dictionary.Code: Select all
Dim tmpdic As Scripting.Dictionary
I don't know of a way to sort a dictionary directly; the array is used to sort the keys.
HansV wrote:Add the following procedure:
To sort your dictionary by key, callCode: Select all
Sub SortDict(dic As Scripting.Dictionary) Dim i As Long Dim j As Long Dim tmp As String Dim arr() As String Dim tmpdic As Scripting.Dictionary ' Get out if dictionary is empty If dic Is Nothing Then Exit Sub End If ' or if it has at most one item If dic.Count <= 1 Then Exit Sub End If ' Fill arrays with arr ReDim arr(0 To dic.Count - 1) For i = 0 To dic.Count - 1 arr(i) = dic.Keys(i) Next i ' Sort the array For i = LBound(arr) To UBound(arr) - 1 For j = i + 1 To UBound(arr) If arr(i) > arr(j) Then tmp = arr(i) arr(i) = arr(j) arr(j) = tmp End If Next j Next i ' Fill new dictionary sorted by key Set tmpdic = New Scripting.Dictionary For i = 0 To dic.Count - 1 tmpdic.Add Key:=arr(i), Item:=dic.Item(arr(i)) Next i ' Set original dictionary to temp one Set dic = tmpdic End Sub
SortDict MYDIC