vba - 如何覆盖 scripting.dictionary 中键的值?

标签 vba excel

我在 for 循环中嵌套了以下 if 子句:

    If status Like "*Active*" Then
        total_active = total_active + amount
        On Error Resume Next
        active_dict.Add Key:=cost_center, Item:=total_active
        On Error GoTo 0
    end if
On Error Resume Next用于忽略 active_dict 中已存在的键时的错误字典正在被添加到它。

问题是 key ( total_active ) 的值 ( cost_center ) 没有更新。

问题是:是否可以覆盖字典键的值?

最佳答案

如果您使用 Microsoft 脚本字典,则可以使用以下内容:

If status Like "*Active*" Then
    total_active = total_active + amount
    If active_dict.Exists(cost_center) Then
        active_dict(cost_center) = total_active
    Else
        active_dict.Add Key:=cost_center, Item:=total_active
    End If
End If

甚至更好(参见 Kostas K 评论):
If status Like "*Active*" Then
    total_active = total_active + amount
    active_dict(cost_center) = total_active
End If

关于vba - 如何覆盖 scripting.dictionary 中键的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50585256/

相关文章:

excel - 将 A 列数据的行连接到第一行 B 列的单个单元格时面临的问题

excel - Microsoft Excel 以日期格式复制和粘贴

excel - 使用 EOMONTH 系列将开始日期扩展到结束日期

vba - 使用列引用清除 VBA 中单元格的内容

vba - 循环浏览选定的幻灯片并删除命名形状

string - 使用 Excel-VBA 获取 HTML 源代码

C# Interop Non-invocable 成员 'Microsoft.Office.Interop.Excel.Range.End' 不能像方法一样使用

使用 Power 查询将 Json 转为 Excel

vba - 通过Excel VBA作为电子邮件附件发送的文件始终损坏

excel - 单元格匹配和返回偏移单元格