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
热门推荐
五当召美岱召壁画揭秘:阿底峡尊者形象的宗教寓意
企业客户关系管理:六大沟通状态助力销售转化
《蛟龙行动》宣传翻车,因涉及机密背台词需警惕四周,网友直言浮夸
从目标到激励:构建高效团队的全方位指南
胡荣华教你象棋开局布局的实战技巧
32句名人金句:从稻盛和夫到村上春树的人生智慧
文字刻录到智能提醒:全方位保持目标专注力
女性脾胃调理指南:五大方面全面改善健康
从饮食到就医:全方位应对抑郁症的躯体症状
洛杉矶大火最新救援进展:14000名消防员全力奋战
解密猪头肉:从中医“发物”到现代营养分析
关乎你的钱袋子!临近春节,这些事没做的抓紧
喜马拉雅“小西藏”达拉克:文化传承面临考验
阿尔及利亚椰枣:从“沙漠面包”到“绿色黄金”
迪拜归来带椰枣,养生秘诀大揭秘
工会在维护劳工权益方面的作用有哪些?
香菇炒肉的做法,简单易学
香菇被称为菇中皇后 教你几招 慧眼识菇
香格里拉大环线14天深度游:成都出发,纵览川西精华
ChatGLM-v2和中文Bloom:AI助手NLP技术的新突破
沟通技巧升级,告别误解烦恼
微信好友删你了?教你优雅应对
西红柿鸡蛋这样挑最好:一看二摸三闻,教你买到优质食材
馒头储存有讲究:冷冻比冷藏更健康
大米该放冰箱冷藏还是冷冻?不同大米储存方式的优劣分析
厨房纸巾巧保存,馒头30天依旧松软
四步保存馒头不变硬,四招蒸出松软口感
铁桶储存汽油,安全无忧!
冬季储油秘籍:阴凉干燥+专业金属容器
冬季行车必备:汽油泄漏应急指南