vba 错误此键已与此集合的元素相关联
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21573320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error This key is already associated with an element of this collection
提问by Gauranga
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
. Can anyone please tell the issue? I even used Cstr(activecell.value), Cstr(val)
as mentioned in one of the answer on similar issue.
我正在研究 vba 宏。我试图使用字典。但它error 457
通过调试器指向toprow.Add ActiveCell.value, val
. 任何人都可以请告诉这个问题吗?我什至使用Cstr(activecell.value), Cstr(val)
了类似问题的一个答案中提到的方法。
Dim toprow As New Dictionary, Dictkey As Variant
Dim val As String
Range("A1").Activate
i = 0
Do Until i = ColLen
val = Chr(65 + i)
toprow.Add ActiveCell.value, val
i = i + 1
ActiveCell.Offset(0, 1).Activate
Loop
回答by AutomatedChaos
Add
ing keys with dictionaries is only possible when a key does not already exist. Accidentally you could entered the key before, oryou 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).
Add
仅当键不存在时,才可以使用字典使用键。偶然地,您可能之前输入了密钥,或者您是watching
调试观察者的密钥,从而立即创建了密钥。(= 如果您查看字典中的某个键,如果它不存在,它就会被创建)。
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 thed.Add keyname, value
method - alternatively you can default to overwrite existing keys by using
d.Item(keyname) = value
- 确保您没有使用调试器查看密钥
- 通过测试创建唯一条目
d.Exists(keyname)
,然后使用该d.Add keyname, value
方法 - 或者,您可以默认使用以下方法覆盖现有密钥
d.Item(keyname) = value
回答by Jaime Montoya
I was getting the same error message: "Error This key is already associated with an element of this collection". In my case, the problem was that I had this:
我收到相同的错误消息:“错误此键已与此集合的元素相关联”。就我而言,问题是我有这个:
'assign values to properties
Property Let EmployeeName(Valor As String)
m_employeename = Valor
End Property
Property Let EmployeeID(Valor As String)
m_employeename = Valor
End Property
I was supposed to have this:
我应该有这个:
'assign values to properties
Property Let EmployeeName(Valor As String)
m_employeename = Valor
End Property
Property Let EmployeeID(Valor As String)
m_employeeid = Valor
End Property
Maybe you just have to double check your "Property Let" code to see if you are using appropriate names for those variables that are private in your class.
也许您只需要仔细检查您的“Property Let”代码,看看您是否为类中私有的变量使用了适当的名称。
回答by bpboldin
You can also add some very basic error handling, if all you wish to do is skip over the record throwing this error. I simply inserted the below line immediately above the one which was generating this error for me, and now it happily moves along, ignoring duplicate keys which used to throw this error.
您还可以添加一些非常基本的错误处理,如果您只想跳过抛出此错误的记录。我只是在为我生成这个错误的那一行的正上方插入了下面的一行,现在它愉快地移动了,忽略了用来抛出这个错误的重复键。
On Error Resume Next