36、[VBA入门到放弃笔记] 字典:属性

2017-06-30  本文已影响126人  叶知行

字典作为一个对象,自然有其属性。字典有COUNT/KEY/ITEM/COMPAREMODE四个属性。

Paste_Image.png
Sub a()
    Dim arr, d As Object
    Set d = CreateObject("scripting.dictionary")
    arr = [a1].CurrentRegion
    '----------新增字典d的条目-----------------------
    For i = 2 To UBound(arr)
        d(arr(i, 1)) = arr(i, 2)
    Next
  n = d.Count                    'n=7
End Sub

Sub a()
    Dim arr, d As Object
    Set d = CreateObject("scripting.dictionary")
    arr = [a1].CurrentRegion
    '----------新增字典d的条目-----------------------
    For i = 2 To UBound(arr)
        d(arr(i, 1)) = arr(i, 2)
    Next
  d.Key("砖头") = "金砖"  '改变key关键字,砖头--->金砖,item不变。
  s = d("金砖")
  ss = d("砖头")
End Sub
Paste_Image.png
Sub a()
    Dim arr, d As Object
    Set d = CreateObject("scripting.dictionary")
    arr = [a1].CurrentRegion
    '----------新增字典d的条目-----------------------
    For i = 2 To UBound(arr)
        d(arr(i, 1)) = arr(i, 2)
    Next
  d.Item("砖头") = 110
  s = d("砖头")
End Sub
Paste_Image.png
Sub a()
    Dim arr, d As Object
    Set d = CreateObject("scripting.dictionary")
    arr = [a1].CurrentRegion
    '----------新增字典d的条目-----------------------
    For i = 2 To UBound(arr)
        d(arr(i, 1)) = arr(i, 2)
    Next
  d("砖头") = 110   '省略了  .item
  s = d("砖头")
End Sub
Paste_Image.png
数据源
Sub ff()
Set d = CreateObject("scripting.dictionary")
    For i = 1 To 4
        d(Cells(i, 1).Value) = Cells(i, 2)
    Next
    s = d.keys
    ss = d.items
End Sub
Paste_Image.png
Sub ff()
Set d = CreateObject("scripting.dictionary")
d.comparemode = 1  '增加此句
    For i = 1 To 4
        d(Cells(i, 1).Value) = Cells(i, 2)
    Next
    s = d.keys
    ss = d.items
End Sub
Paste_Image.png
上一篇 下一篇

猜你喜欢

热点阅读