Count Method
Returns the number of items in a collection or Dictionary object. Read-only.
Syntax
object.Count
Arguments
Object: Required. Always the name of a Dictionary
object.
Remarks
The following code illustrates use of the Count
property:
Example 1:
Function ShowItemCount
Dim d, count
Set d = CreateObject("Scripting.Dictionary")
' Add some keys and items.
d.Add "a", "Athens"
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
' Get the item count.
count = d.Count
ShowItemCount = "The dictionary contains " & count & " items."
End Function
' Usage
Dim result
result = ShowItemCount
' Output: The dictionary contains 3 items.
Example 2:
Function DisplayItemsWithCount
Dim d, i, s
Set d = CreateObject("Scripting.Dictionary")
' Add some keys and items.
d.Add "x", "Xenon"
d.Add "y", "Yttrium"
d.Add "z", "Zirconium"
' Get the item count.
Dim count
count = d.Count
' Display each item.
s = "The dictionary contains " & count & " items:"
For Each Key In d.Keys
s = s & "Key: " & Key & ", Item: " & d.Item(Key)
Next
DisplayItemsWithCount = s
End Function
' Usage
Dim result
result = DisplayItemsWithCount
' Output: The dictionary contains 3 items: Key: x, Item: Xenon Key: y, Item: Yttrium Key: z, Item: Zirconium
Example 3:
Function CountAfterRemoval
Dim d, count, s
Set d = CreateObject("Scripting.Dictionary")
' Add some keys and items.
d.Add "m", "Moscow"
d.Add "n", "New York"
d.Add "p", "Paris"
' Remove a key-item pair.
d.Remove("n")
' Get the item count.
count = d.Count
' Display the count and remaining items.
s = "After removal, the dictionary contains " & count & " items:"
For Each Key In d.Keys
s = s & "Key: " & Key & ", Item: " & d.Item(Key)
Next
CountAfterRemoval = s
End Function
' Usage
Dim result
result = CountAfterRemoval
' Output: After removal, the dictionary contains 2 items: Key: m, Item: Moscow Key: p, Item: Paris