gpt4 book ai didi

Excel VBA : This key is already associated with an element of this collection

转载 作者:行者123 更新时间:2023-12-04 20:55:50 25 4
gpt4 key购买 nike

目标:

我有一些变量已声明为变体,我想将它们添加到字典中,以便循环遍历它们并动态地向它们添加值。我希望它们使用 Null 的值进行初始化,因为稍后我会将它们传递到 sql 语句中,如果没有可用的值,它们需要为 Null。这些值将来自 Excel 中的一系列单元格。

错误:

Run-time error '457':

This key is already associated with an element of this collection



代码:
Dim Entity As Variant
Dim Project As Variant
Dim Status As Variant
Dim Draft As Variant
...

Set d = CreateObject("Scripting.Dictionary")

d.Add Entity, Null
d.Add Project, Null <- Debugger highlights this row
d.Add Status, Null
d.Add Draft, Null
...

我试过的:
  • Casting the key, values as a string w/CSTR()
  • 最佳答案

    我的解决方案:

    将键添加为字符串,然后执行任何逻辑。然后,将字典中的值传递回要使用的变量。

    代码:

    Dim Entity As Variant
    Dim Project As Variant
    Dim Status As Variant
    Dim Draft As Variant
    ...

    Set d = CreateObject("Scripting.Dictionary")

    d.Add "Entity", Null
    d.Add "Project", Null
    d.Add "Status", Null
    d.Add "Draft", Null
    ...

    ' Pass the values into the variable after manipulation
    Entity = d("Entity")
    Project = d("Project")
    Status = d("Status")
    Draft = d("Draft")

    关于Excel VBA : This key is already associated with an element of this collection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47835179/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com