各位前輩早安
昨天在論壇學習字典發現差異!
先講目前認知的結論,請各位前輩指正並指導!謝謝 任何非 Exists() 方法查詢字典的步驟程序可能會變動字典的數量?? 1.標準的 Dictionary 物件中搜尋的索引 鍵 值
For i = 1 To 20
If Y.Exists(i) Then
2.偷懶的 Dictionary 物件中搜尋的索引 鍵 值
For i = 1 To 20
If Y(i) = "" Then
心得差異說明如下: 偷懶的方式
Sub TEST_Count_1()
Dim Y, i, x, j
Set Y = CreateObject("Scripting.Dictionary")
Y(1) = 1
MsgBox Y.Count
For i = 1 To 20 If Y(i) = "" Then
End If
Next
MsgBox Y.Count
For Each x In Y.KEYS
j = j + 1
Next
MsgBox j
End Sub
視窗會顯示 1 → 20 → 20
-------------------------------------------------- 標準的方式
Sub TEST_Count_2()
Dim Y, i, x, j
Set Y = CreateObject("Scripting.Dictionary")
Y(1) = 1
MsgBox Y.Count
For i = 1 To 20 If Y.Exists(i) Then
End If
Next
MsgBox Y.Count
For Each x In Y.KEYS
j = j + 1
Next
MsgBox j
End Sub