在词典的excel里面引用词典的Dictionary内部 – VBA Object Required Error

正确,所以我有一个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相当陌生,但是Im试图在这里做的是在多个dictionarys级别中引用gradeCount,并将值增加1。

Dictionary对象接受对象引用作为键。 因为这个原因,当你input一个Range object作为关键字的项时,你不能使用range's value作为关键字来检索相同的项目,它将不会find它。 如果添加具有值的项目并尝试通过范围对象(引用)来检索它,则同样适用。

作为一般规则,如果.Add中的键是一个Range ,则使用相同的范围对象作为.Item的键。 另一方面,如果.Add中的键是一个Range.Value (这是你真正想要做的),在.Item使用相同的value作为键。

 dict.Item(rubricItemC.Value).Item(subRubricItem.Value).Item(ratCell.Value) = dict.Item(rubricItemC.Value).Item(subRubricItem.Value).Item(ratCell.Value) + 1