首页 > 解决方案 > 为什么VB在遍历集合时抛出

问题描述

我收到以下代码的“对象变量或未设置块变量”。

Set entries = FindAllMatches(interestWorksheets, input_chapter_code)
For Each x In entries
   If x.Value = input_amount.Value Then // VBA STOPS HERE
   End if
Next

调试器显示entries有 4 个Range元素,而xis Nothing(WHAT???)

FindAllMatches定义如下:

Function FindAllMatches(worksheets, input_chapter_code) As Collection
    Dim list As New Collection
    For Each ws In worksheets
        list.Add Item:=FindMatchInWorksheet(ws, input_chapter_code)
    Next
    Set FindAllMatches= list
End Function

Function FindMatchInWorksheet(worksheet, input_chapter_code) As Range
...
End Function

标签: excelvba

解决方案


推荐阅读