範例如下:
Sub aa()
'測試資料 E_Data01表
Dim myAr As Variant
Dim myCnt As Long
Dim myClc As New Collection
Dim myKey As String
Dim myErrNum As Long
Dim i As Long
Dim mStr1$, mStr2$
myAr = Range("A1").CurrentRegion.Value '指定搜尋來源範圍
For i = 1 To UBound(myAr)
'myClc.Add Item:=myAr(i, 8), key:=CStr(myAr(i, 3))
myClc.Add Item:=myAr(i, 8) & "_" & myAr(i, 3), key:=CStr(myAr(i, 3)) '如果改變Item的內容為何就無法指出key值呢?
Next
For i = 1 To myClc.Count
mStr1 = myClc.Item(i)
Next
myKey = "渡邊" '指定KEY
On Error Resume Next
myCnt = myClc.Item(myKey) '如果item改變時,myCnt的值就無法取得。
'myCnt = myClc(myKey)
myErrNum = Err.Number
On Error GoTo 0
If myErrNum = 0 Then
MsgBox myKey & "的合計分數為" & myCnt & "。"
Else
MsgBox "沒有找到符合條件的資料"
End If
Set myClc = Nothing '物件的釋放
End Sub