VBA字典高效数据处理技巧分享
创作时间:
作者:
@小白创作中心
VBA字典高效数据处理技巧分享
Sub DictionaryExample()
' 创建字典对象
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
' 添加键值对
dict.Add "A", 100
dict.Add "B", 200
dict.Add "C", 300
' 检查键是否存在
If dict.Exists("B") Then
MsgBox "Key B exists. Value: " & dict("B")
End If
' 修改值
dict("B") = 250
' 删除键
dict.Remove "C"
' 遍历字典
Dim key As Variant
For Each key In dict.Keys
Debug.Print "Key: " & key & ", Value: " & dict(key)
Next
' 统计元素数量
MsgBox "Total items: " & dict.Count
' 清空字典
dict.RemoveAll
End Sub
Sub DictionaryAdvancedUsage()
' 创建字典对象
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
' 数据去重示例
Dim arr As Variant
arr = Array("apple", "banana", "apple", "orange", "banana", "grape")
Dim i As Long
For i = LBound(arr) To UBound(arr)
dict(arr(i)) = 1 ' 任意值,关键是键的唯一性
Next
' 去重后的数组
Dim uniqueArr() As String
ReDim uniqueArr(dict.Count - 1)
i = 0
For Each key In dict.Keys
uniqueArr(i) = key
i = i + 1
Next
' 输出去重后的数组
For i = LBound(uniqueArr) To UBound(uniqueArr)
Debug.Print uniqueArr(i)
Next
' 数据查找示例
Dim searchKey As String
searchKey = "banana"
If dict.Exists(searchKey) Then
MsgBox "Found: " & searchKey
Else
MsgBox "Not found: " & searchKey
End If
' 字典排序示例
Dim sortedKeys() As Variant
sortedKeys = dict.Keys
Call QuickSort(sortedKeys, LBound(sortedKeys), UBound(sortedKeys))
' 输出排序后的键值对
For i = LBound(sortedKeys) To UBound(sortedKeys)
Debug.Print "Sorted Key: " & sortedKeys(i) & ", Value: " & dict(sortedKeys(i))
Next
' 多字典操作示例
Dim dict2 As Object
Set dict2 = CreateObject("Scripting.Dictionary")
dict2.Add "D", 400
dict2.Add "E", 500
' 合并字典
Dim combinedDict As Object
Set combinedDict = CreateObject("Scripting.Dictionary")
For Each key In dict.Keys
combinedDict.Add key, dict(key)
Next
For Each key In dict2.Keys
combinedDict.Add key, dict2(key)
Next
' 输出合并后的字典
For Each key In combinedDict.Keys
Debug.Print "Combined Key: " & key & ", Value: " & combinedDict(key)
Next
End Sub
' 快速排序算法用于排序数组
Sub QuickSort(arr() As Variant, first As Long, last As Long)
Dim pivot As Variant
Dim temp As Variant
Dim i As Long
Dim j As Long
If first < last Then
pivot = arr((first + last) \ 2)
i = first - 1
j = last + 1
Do
Do
i = i + 1
Loop While arr(i) < pivot
Do
j = j - 1
Loop While arr(j) > pivot
If i <= j Then
temp = arr(i)
arr(i) = arr(j)
arr(j) = temp
End If
Loop While i <= j
QuickSort arr, first, j
QuickSort arr, j + 1, last
End If
End Sub
热门推荐
五行命中缺火意味着什么命格
脑动静脉畸形影像学检查方法
警惕!苹果App Store首次出现OCR恶意应用,如何防范?
都知道CPU、GPU,那NPU、DPU、TPU、IPU又是个啥?
维生素B6为什么被称为“万能钥匙”?
公积金对冲还贷条件及办理流程详解
怎么区别急性咽喉炎和慢性咽喉炎
粪便干湿分离机是什么
常用的非甾体抗炎药有哪些
苏州社保公积金缴纳标准2024年最新,苏州五险一金缴纳基数和比例
7 种实现 CSS 三角形的原理与方法 以及 三角形在网页设计中的作用
个人养老金账户可以注销吗
全球视野:哪些国家对双重国籍持开放态度?
流感≠感冒!常被忽视的流感到底该如何应对?
服务器环境配置需要满足哪些要求?
石锅拌饭家常做法:两种不同版本,让你轻松做出正宗韩式美味
历史上的南昌竟然有过这么多名字
如何面对现实的残酷?从个人经历谈建立自信的方法
解读:公司未足额缴纳社保,可以要求公司补缴社保吗?
四幅广为流传朱元璋画像,哪一幅才是真实的?
燕麦片好消化吗
房屋租赁合同示范文本及律师填写指南
微喷带的使用年限与水质有关
鲨鱼肉能吃吗?营养价值与食用指南全解析
大额逆回购到期,央行今日回笼7165亿,资金面维持紧平衡
补铁别只知道吃红枣!真正补铁的 23 种食物都在这里
雪碧鸡丝凉面的做法
历史与现状: 妈祖文化在中国大陆和东南亚之间的交流互动
关于中医穴位埋线减肥,你想知道的都在这儿!
2025科技、传媒和电信行业预测:生成式AI将带来重大转折