我在 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/