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
热门推荐
案例研究:引力波的发现
专业心理医生详解:中度抑郁症失眠的综合治疗方案
红霉素软膏治痘真的有效吗?
苏轼《记承天寺夜游》原文、注释、译文、赏析
小巷人家黄玲:从忍耐到反抗,东方女性的坚韧人生
雪菜肉丝包制作教程:馅料调制到蒸制全解析
老爸教你如何预防膝盖积水
招财鱼养殖秘籍:科学养护,长寿无忧!
难忘的丽江之旅(附加攻略)
佛诞日期存争议,浴佛节习俗代代传
百会穴:人体重要穴位的位置、功效与按摩方法
生僻字取名指南:15个蕴藏美好寓意的罕见汉字
归脾丸配六味地黄丸:医生教你安全联用这两种常见中药
从粥到糕点,红米的十种营养美味做法
降压护心又经济,马来酸依那普利片成高血压治疗优选
从红烧肉到宫保鸡丁:九道经典中式菜肴制作详解
按摩是否能够有效淡化眼袋?探讨其原理与结果。
劳动者必知:N、N+1、2N经济补偿金计算详解
上海外地车牌限行指南:多种渠道申请限行通行证
血氧饱和度100%,真的健康吗?
聚丙烯酰胺用途有哪些
头重如裹?中医教你快速摆脱湿邪困扰
中国地图全览:行政区划、地形气候与资源分布
低糖版糖醋汁,健康吃法大公开!
糖醋汁12345黄金比例,大厨秘籍公开!
如何使用114挪车电话?四步完成,隐私无忧
文学中的幽默:从语言游戏到社会批判,点亮阅读乐趣
SD卡无法读取怎么修复?这4种修复方法你务必知晓!亲测有效!
雪茄烟官方购买渠道全攻略:专柜、官网到电商平台
一颗金橘里的养生智慧:理气消食,寓意吉祥