错误此键已与此集合的元素相关联 [英] Error This key is already associated with an element of this collection

查看:420
本文介绍了错误此键已与此集合的元素相关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用vba宏。我试图使用一个字典。但是它正在提供错误457 ,调试器指向 toprow.Add ActiveCell.value,val 。任何人都可以告诉这个问题?我甚至在类似问题的答案之一中使用了 Cstr(activecell.value),Cstr(val)



$

范围(A1)激活
i = 0
Do Until i = ColLen
val = Chr(65 + i)
toprow.Add ActiveCell.value,val
i = i + 1
ActiveCell.Offset 0,1)。激活
循环


解决方案

只有当一个键不存在时才可以使用字典添加 ing键。意外地,您可以在之前输入密钥,您正在使用调试观察器查看键,以便即时创建密钥。 (=如果您在字典中观看某个键,如果不存在则创建它)。



您必须




  • 确保您没有使用调试器观看密钥

  • 通过在上测试创建唯一条目d.Exists (keyname)然后使用 d.Add keyname,value 方法

  • 或者你可以默认为使用覆盖现有密钥d.Item(keyname)= value


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.

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

解决方案

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

这篇关于错误此键已与此集合的元素相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆