对,所以我有一个循环多个 pdf 并从中提取数据的 excel 程序。为了使其工作,它使用一个指标来查看命名范围的关键值,即 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 相当陌生,但我在这里尝试做的是在多个级别的字典中引用 GradeCount 并将值增加 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
关于vba - 在字典的 excel 中引用字典的内部 - VBA 对象需要错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33739011/