"This key is already associated with an element of this collection"和运行时错误 457



原始帖子:
已验证字典中正在索引的数据在索引列中没有重复值
范围中的数据格式为:col 1=索引(字符串(| col 2=值(双精度(
在运行此代码段之前,所有值都设置为0
错误捕获设置为"未处理错误中断">
为什么会出现此错误?我找不到任何能把副本扔进字典的东西。(对不起,我不是开发者。(



更新:
不再获得重复键错误,但现在除了所需的索引键外,还可以看到添加到字典中的值(以及随后的输出(作为键。如果这是一大碗意大利面条,我深表歉意
下方的完整代码

Sub CompileActuals()
' Get it started
Dim ibh As Integer
Dim pah As Integer
Dim WrkSht As Worksheet
Dim RngB As Range
Dim RngA As Range
Dim CLine As Object
Dim CLineData As Class1
Dim im As Integer
Dim cm As Integer
Dim vpah As Integer
Dim vibh As Integer
Dim vMonth As Integer
Dim vO As Integer
Dim LineWeight As Double
Dim varIndex As Variant
ibh = Sheets("GCM Compiler").Cells(1, 6)
pah = Sheets("GCM Compiler").Cells(2, 6)
im = Sheets("GCM Compiler").Cells(3, 4)
cm = Sheets("GCM Compiler").Cells(1, 4)
Set WrkSht = ThisWorkbook.Worksheets("GCM Compiler")
Set RngB = WrkSht.Range(Cells(9, 4 + (2 * im)), Cells(1563, 5 + (2 * im)))
Set RngA = WrkSht.Range(Cells(9, 3), Cells(8 + pah, 4))
Set CLine = CreateObject("Scripting.Dictionary")
' Wipe Budget Numbers
Range(Cells(9, (5 + (2 * im))), Cells(8 + ibh, 5 + (2 * im))).Value = 0
' Clear the Dictionary
CLine.RemoveAll
' Fill dictionary with zeros for the existing indices
For vibh = 1 To ibh
' Set each row's variables
varIndex = RngB.Cells(vibh, 1).Value
LineWeight = CDbl(RngB.Cells(vibh, 2).Value)
vMonth = cm
' Test for index already exists in dictionary
If CLine.Exists(varIndex) Then
' Get existing index entry's properties
Set CLineData = CLine(varIndex)
' Add weight of that line to the index entry
CLineData.Weight = 0
Else
' Create an entry for the new index
Set CLineData = New Class1
' Set properties of the new entry
CLineData.Weight = LineWeight
CLineData.Month = vMonth
CLineData.Index = varIndex
' Store the new entry in the dictionary
CLine.Add varIndex, CLineData
End If
Next vibh
' Add actuals, iterate thru pasted actual values
For vpah = 1 To pah
' Set each row's variables
varIndex = RngA.Cells(vpah, 1).Value
LineWeight = CDbl(RngA.Cells(vpah, 2).Value)
vMonth = cm
' Test for index already exists in dictionary
If CLine.Exists(varIndex) Then
' Get existing index entry's properties
Set CLineData = CLine(varIndex)
' Add weight of that line to the index entry
CLineData.Weight = CLineData.Weight + CDbl(RngA.Cells(vpah, 2))
Else
' Create an entry for the new index
Set CLineData = New Class1
' Set properties of the new entry
CLineData.Weight = LineWeight
CLineData.Month = vMonth
CLineData.Index = varIndex
' Store the new entry in the dictionary
CLine.Add varIndex, CLineData
End If
Next vpah
' Output Compiled Weights
For vO = 0 To CLine.Count - 1
Set CLineData = CLine.Items()(vO)
WrkSht.Cells(9 + vO, 4 + (2 * im)).Value = CLineData.Index
WrkSht.Cells(9 + vO, 5 + (2 * im)).Value = CLineData.Weight
Next vO

'
End Sub

数据有两个范围,预算和实际值。该代码的目标是将预算替换为零,然后编译现有关键项和新关键项的实际值,并将数据添加到预算数据的原始位置。
实际值和预算输入数据范围看起来都是这样的,索引在第一列,关联值在第二列:
Key 1| value
Key2| value
Key 3| value
Key 1|value
Key 4| value

只需使用以下内容:

If Not .Exists(SomeKey) Then 
.Add SomeKey, SomeValue
Else
stop 'Go investigate what someKey is
End If

此外,我建议在https://www.experts-exchange.com/articles/3391/Using-the-Dictionary-Class-in-VBA.html

相关内容

最新更新