是的,所以我有一个excel程序,它循环遍历多个pdf并从中提取数据。为了实现这一点,它使用了一个标题来查看命名范围的关键值,即RubricItems,RatingsValuesRow和RatingsColumn。我必须使用命名范围,因为量规可能会在任何给定时间发生变化。贝娄是我遇到问题的代码片段。
对于此rubricItemC.value = 1,subRubricItem = a,ratCell = 4
Dim ratingsCol As Range
Dim ratingsVal As Range
Dim rubricItem As Range
Dim rubricItemC As Range
Dim subRubricItem As Range
Dim gradeCount As Integer
Dim c As Range
Dim ratCount As Range
Dim ratCell As Range
count = 0
gradeCount = 0
Set rubricItem = Workbooks(strRubricTemplateFilename).Worksheets(RubricSheet).Range("RubricItems")
Set ratingsVal = Workbooks(strRubricTemplateFilename).Worksheets(RubricSheet).Range("RatingsValuesRow")
Set ratingsCol = Workbooks(strRubricTemplateFilename).Worksheets(RubricSheet).Range("RatingsColumn")
'populates the ratings values which consist of [X,1,2,3,4]
For Each c In ratingsVal.Cells
If Not (c.Value = "") Then
gradeValuesDict.Add c.Value, gradeCount
End If
Next
'iterates through each item in the rubric
For Each c In rubricItem.Cells
Set rubricItemC = c
Set ratCell = Cells(rubricItemC.Row, ratingsCol.Column)
Set subRubricItem = rubricItemC.offset(0, 1)
'checks to see if the dictionary exist if not create it.
If Not dict.Exists(rubricItemC.Value) Then
'adds to the dictionary passing another dictionary as the item.
dict.Add rubricItemC.Value, subRubricDict
End If
'checks to see if the sub dictionary exists if not create it.
If Not dict.Item(rubricItemC.Value).Exists(subRubricItem.Value) Then
dict.Item(rubricItemC.Value).Add subRubricItem.Value, gradeValuesDict
End If
dict.Item(rubricItemC.Value).Item(subRubricItem).Item(ratCell) = dict.Item(rubricItemC.Value).Item(subRubricItem).Item(ratCell) + 1
Next
这是我收到对象所需错误的地方。
dict.Item(rubricItemC.Value).Item(subRubricItem).Item(ratCell) = dict.Item(rubricItemC.Value).Item(subRubricItem).Item(ratCell) + 1
我对VBA相当新,但我在这里尝试做的是在多个级别的dictionarys中引用gradeCount并将值增加1.
答案 0 :(得分:1)
Dictionary对象接受对象引用作为键。因此,当您输入一个以Range object
作为键的项目时,您无法使用range's value
作为键检索相同的项目,它将无法找到它。如果添加具有值的项目并尝试通过范围对象(引用)检索它,则同样适用。
作为一般规则,如果.Add
中的密钥为Range
,请使用相同的范围对象作为.Item
中的密钥。另一方面,如果.Add
中的密钥为Range.Value
(这是您实际想要执行的操作),请使用与value
中的密钥相同的.Item
。
dict.Item(rubricItemC.Value).Item(subRubricItem.Value).Item(ratCell.Value) = dict.Item(rubricItemC.Value).Item(subRubricItem.Value).Item(ratCell.Value) + 1