VBA 字典 Dictionary 按Key排序(利用System.Collections.ArrayList)

原创文章,欢迎转载:http://miao.blog/article/vba-dictionary-sort-by-key

' sort dictionary, sortBy: ASC, DESC
Function SortDictionary(dic As Dictionary, Optional sortBy As String = "ASC")
    
    ' create ArryaList Object
    Dim objArrayList As Object: Set objArrayList = CreateObject("System.Collections.ArrayList")
    
    ' add dictionary key to ArrayList
    Dim key
    For Each key In dic.Keys
        objArrayList.Add key
    Next
    
    ' Sort (ASC)
    objArrayList.Sort
    
    ' create a new dic
    Dim new_dic As Dictionary: Set new_dic = New Dictionary
    
    ' sort by ASC
    If sortBy = "ASC" Then
        For i = 0 To objArrayList.count - 1
            key = objArrayList(i)
            new_dic.Add key, dic(key)
        Next
    ' sort by desc
    Else
        For i = objArrayList.count - 1 To 0 Step -1
            key = objArrayList(i)
            new_dic.Add key, dic(key)
        Next
    End If
    
    Set SortDictionary = new_dic
End Function
推荐阅读
获取Excel的AddIn里边的Sheet对象
vba要实现md5必须引用外部库,导致vba程序交付安装不是很方便。很多时候我们并不是想要取真正的md5,只是取一个加密字符串,比如保存密码的时候。本文中的代码直接利用windows系统内置命令certutil -hashfile实现了一种变相的md5函数。代码要读写本地文件两次,因此效率不高,不过在登陆验证一下密码等场景并没有影响。
VBA发送GET、POST请求的方法
评论
sss

sss

2019-12-25
public static void main(String[] args) {
System.out.println("hello world!!")
}
jason

jason

2020-05-29

VBA显示用户定义类型未定义