Vous pouvez créer une petite classe pour contenir la clé et la valeur, puis stocker les objets de cette classe dans la collection.
Classe KeyValue :
Public key As String
Public value As String
Public Sub Init(k As String, v As String)
key = k
value = v
End Sub
Puis de l'utiliser :
Public Sub Test()
Dim col As Collection, kv As KeyValue
Set col = New Collection
Store col, "first key", "first string"
Store col, "second key", "second string"
Store col, "third key", "third string"
For Each kv In col
Debug.Print kv.key, kv.value
Next kv
End Sub
Private Sub Store(col As Collection, k As String, v As String)
If (Contains(col, k)) Then
Set kv = col(k)
kv.value = v
Else
Set kv = New KeyValue
kv.Init k, v
col.Add kv, k
End If
End Sub
Private Function Contains(col As Collection, key As String) As Boolean
On Error GoTo NotFound
Dim itm As Object
Set itm = col(key)
Contains = True
MyExit:
Exit Function
NotFound:
Contains = False
Resume MyExit
End Function
Ceci est bien sûr similaire à la suggestion du dictionnaire, mais sans aucune dépendance externe. La classe peut être rendue plus complexe si nécessaire, si vous souhaitez stocker plus d'informations.