![efe71f1317d4a3c4e610074bd3dc4b88.png](https://i-blog.csdnimg.cn/blog_migrate/395bd6f32387fcb69fcce3cf9437d6d7.jpeg)
大家好,今日推出常用“积木”过程案例分享第315期,今日内容是利用字典实现三条件,结果多值查询。VBA是利用Office实现个人小型办公自动化的有效手段(工具)。这些资料就是我多年经验的记录,都来源于我多年的实践。大家在学习VBA的时候,可以把这些代码块作为一块块的积木对待,平时积累,用时拿来修正、组合。这就是我的“积木编程”的思想,代码不要自己全部的录入。建立自己的“积木库”。这些讲解就是我推出的“积木”方案,希望大家加以利用。最近代码多是出自我的第三套教程”VBA数组与字典解决方案”。
![597ce24717d05917af4097fdbbd18344.png](https://i-blog.csdnimg.cn/blog_migrate/94089c0bc3e97c3a8ecaa9ec7201f15c.jpeg)
=========================== ① =========================
Sub mynzsz_84()
Sheets("84").Select
myarr = Range("a2:f" & Range("a2").End(xlDown).Row)
Set mydic = CreateObject("Scripting.Dictionary")
For i = 1 To UBound(myarr)
If Not mydic.exists(myarr(i, 1)) Then
Set mydic(myarr(i, 1)) = CreateObject("Scripting.Dictionary")
End If
If Not mydic(myarr(i, 1)).exists(myarr(i, 2)) Then
Set mydic(myarr(i, 1))(myarr(i, 2)) = CreateObject("Scripting.Dictionary")
End If
mydic(myarr(i, 1))(myarr(i, 2))(myarr(i, 3)) = Array(myarr(i, 4), myarr(i, 5), myarr(i, 6))
Next
Set myRng = Range(Cells(2, "I"), Cells(Range("I2").End(xlDown).Row, "I"))
For Each uu In myRng
Cells(uu.Row, "L") = "": Cells(uu.Row, "N") = "": Cells(uu.Row, "M") = ""
If mydic.exists(uu.Value) Then
If mydic(uu.Value).exists(Cells(uu.Row, "J").Value) Then
Cells(uu.Row, "L").Resize(1, 3) = mydic(uu.Value)(Cells(uu.Row, "J").Value)(Cells(uu.Row, "K").Value)
End If
End If
Next
Set mydic = Nothing
MsgBox ("ok")
End Sub
=========================②========================
代码讲解:
1)上面的代码实现了三条件下多值的返回,其中利用了Array(myarr(i, 4), myarr(i, 5), myarr(i, 6))给三级嵌套的字典赋值,利用了
Cells(uu.Row, "L").Resize(1, 3) = mydic(uu.Value)(Cells(uu.Row, "J").Value)(Cells(uu.Row, "K").Value)
将所查找到的数据结果回填。
2) '在字典中装入数据
For i = 1 To UBound(myarr)
'这里利用了字典的嵌套
If Not mydic.exists(myarr(i, 1)) Then
Set mydic(myarr(i, 1)) = CreateObject("Scripting.Dictionary")
End If
If Not mydic(myarr(i, 1)).exists(myarr(i, 2)) Then
Set mydic(myarr(i, 1))(myarr(i, 2)) = CreateObject("Scripting.Dictionary")
End If
'mydic的键值是字典,这时字典的键值是一个一维数组
mydic(myarr(i, 1))(myarr(i, 2))(myarr(i, 3)) = Array(myarr(i, 4), myarr(i, 5), myarr(i, 6))
Next
上述代码实现了给字典的赋值,赋值数组给三级嵌套的字典,字典三个键分别为要求的三个条件。
3) '填入查到的数据,在字典的嵌套中查找到键值,此时键值是数组,回填到区域
'注意下面语句的写法,不然会报错,字典嵌套时要先判断上层字典是否存在
If mydic.exists(uu.Value) Then
If mydic(uu.Value).exists(Cells(uu.Row, "J").Value) Then
Cells(uu.Row, "L").Resize(1, 3) = mydic(uu.Value)(Cells(uu.Row, "J").Value)(Cells(uu.Row, "K").Value)
End If
End If
Next
上面的代码实现数据的查找和回填,这里我给出了注释:'注意下面语句的写法,不然会报错,字典嵌套时要先判断上层字典是否存在。为什么呢?我们要理解字典的查询好处,是直达目的,不会像FIND那样去查找,如果没有最终的数据时,字典会直接返回空值,但要注意的是上述代码建立了一个三级的嵌套,要查找的最终数据是在最内层的键值中,如果上层没有,不必要再在下层查找了,这就是上面代码的意义。
![1d4b65419387ab1835ab93ece73e748e.png](https://i-blog.csdnimg.cn/blog_migrate/5515bd3fb5981e68c091bd9268f30cc0.png)
![5397e9f35045a30db9d6ecf4109f2a8c.png](https://i-blog.csdnimg.cn/blog_migrate/859997b52b01fea1eea50ce39484af2e.jpeg)
各套教程的介绍:
第1套:VBA代码解决方案
第2套:VBA数据库解决方案
第3套:VBA数组与字典解决方案
第4套:VBA代码解决方案之视频
第5套:VBA中类的解读和利用
第6套:VBA信息获取与处理
上述教程的学习顺序:1→3→2→6→5或者4→3→2→6→5。如需要可以WeChat: VBA6337或者NZ9668
分享成果,随喜正能量