原始帖子:
已验证在字典中建立索引的数据在索引列中没有重复的值。
范围内的数据具有以下格式:col 1 = index(string)| col 2 = value(double)
在此部分代码运行之前,所有值都设置为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
数据分为两个范围,预算和实际值。该代码的目标是将预算替换为零,然后编译现有关键项目和新关键项目的实际值,并将数据添加到预算数据的原始位置。
实际值和预算输入数据范围都将看起来像这样,第一列为索引,第二列为关联值:
关键1 |值
关键2 |值
关键3 |值
关键1 |值
关键4 |值
答案 0 :(得分:0)
只需使用以下内容:
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
上查看出色的文章