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
热门推荐
糖尿病患者的福音:中医5大穴位精准控糖全攻略!
杭州电竞队伍风采:探索本地战队的实力与魅力
城市森林花园住宅!详解漳州“第四代住房”
肿瘤生物标志物及其检测方法的研究前沿及在癌症中的应用前景
微波实验三:微波阻抗的测量与匹配
硅藻泥有什么作用与好处?家居装修新选择
AI时代如何养娃?给家长的国学启蒙行动指南
捡到钱包后该如何处理
近视手术后为什么要戴偏光镜?
购买水稻种子时需要注意的事项
水浒传中董平有哪些经历?他的性格是怎样的?
冬季头部易受寒,保暖方式及重要性你得知道
古代新娘出嫁为何头戴红盖头?
2025年十大逻辑游戏推荐:从数独到空间解谜,全方位提升思维能力
如何维护直流稳压电源以延长使用寿命
多地上线"多车联动掉头" 新型通道 车辆应该怎么走?
长治的美食有哪些?
探究岫玉的矿物成分特性
富人如何制定财务计划:从目标设定到风险管理的全方位解析
国家电网认可的6所学校!附2025年录取大学排名完整版
玻璃养生壶对人体有害吗?警惕四大致癌危害缺陷!
他汀类药物与肾损伤:真相与安全用药指南
上下牙咬合错位怎么矫正?个性化矫正方案轻松get正确咬合,告别上下牙齿咬合难
中国预防出生缺陷日 关注孕期用药安全 避免不良妊娠结局
爬楼梯锻炼下肢力量,真的可行吗?
迷迭香精油的制作方法及其优化研究
深圳各区2024年GDP:龙岗甩掉宝安直追福田
怎样撇清丈夫的债务:证据收集与法律建议
三角洲行动套包机制详解:打造专属战术策略的关键要素!
租车旅游怎样选择才经济实惠?