Error This key is already associated with an element of this collection

前端 未结 3 1490
孤城傲影
孤城傲影 2021-01-02 04:10

I am working on vba macros. I was trying to use a dictionary. But it is giving error 457 with debugger pointing to toprow.Add ActiveCell.value, val

3条回答
  •  -上瘾入骨i
    2021-01-02 04:57

    Adding keys with dictionaries is only possible when a key does not already exist. Accidentally you could entered the key before, or you are watching the key with the debug watcher, creating the key instanteneously. (= If you watch a certain key in a dictionary it gets created if it doesn't already exist).

    You have to

    • make sure you are not watching the key with the debugger
    • create unique entries by testing on d.Exists(keyname) and then use the d.Add keyname, value method
    • alternatively you can default to overwrite existing keys by using d.Item(keyname) = value

提交回复
热议问题