Error 457, “This key is already associated with an element of this collection”, fires when Collection.Add is given a key that the collection already holds. Keys in a VBA Collection must be unique; they are the collection's index-by-name mechanism, and a duplicate is refused outright. The usual story is data that was assumed unique (names, codes, IDs) meeting a real duplicate, or code that adds an item on a path that can run twice for the same item. The collection is protecting its index; the assumption of uniqueness is what failed.
Function HasKey(c As Collection, k As String) As Boolean
Dim probe As Variant
On Error Resume Next
probe = c(k)
HasKey = (Err.Number = 0)
On Error GoTo 0
End Function
If Not HasKey(cache, key) Then cache.Add value, key
UCase$(Trim$(key))) when building keys from data, so “ABC” and “abc ” collide visibly in testing rather than silently becoming two entries for one thing, or one error for two.If the data is provably unique, the same item is being added twice by two code paths (an initialization that runs on both open and first use, for example). Log each Add with its key and call site; the double-add shows up as the same key arriving from two places.
Are collection keys case-sensitive? No. VBA Collection keys compare without regard to case, so “Key1” and “KEY1” are the same key and the second Add raises error 457.
Why is there no simple Exists method on Collection? A long-standing gap in the VBA object model. The trapped-probe pattern is the standard workaround; the Scripting.Dictionary, by contrast, provides a native Exists method.