VBA:错误91对象变量或未设置块变量

VBA:错误91对象变量或未设置块变量,vba,excel,Vba,Excel,我面临一个奇怪的VBA错误问题。我看到很多其他人都有这个问题,因为他们没有为对象使用关键字“Set”,而我的情况不是这样 以下是我的代码: Dim eventWS As Worksheet Set eventWS = Worksheets("Event Sheet") Dim eventRange As Range Set eventRange = eventWS.Columns("A:A").Find(240, , xlValues, xlWhole) If Not eventRange

我面临一个奇怪的VBA错误问题。我看到很多其他人都有这个问题,因为他们没有为对象使用关键字“Set”,而我的情况不是这样

以下是我的代码:

Dim eventWS As Worksheet
Set eventWS = Worksheets("Event Sheet")

Dim eventRange As Range
Set eventRange = eventWS.Columns("A:A").Find(240, , xlValues, xlWhole)

If Not eventRange Is Nothing Then 

      Dim eventFirstAddress As String 
      eventFirstAddress = eventRange.Address 

      Do 
         If eventWS.Range("L" & eventRange.Row).Value = busId Then 
              If commuter = True Then 
                 Count = Count + Affected(eventWS.Range("Q" & eventRange.Row).Value)
              Else
                 Count = Count + 1
              End If 
         End If 
         MsgBox("Before call move next: " & eventRange.Row )
         Set eventRange = eventWS.Columns("A:A").FindNext(eventRange)
         MsgBox("After call move next: " & eventRange.Row )
      Loop While Not eventRange Is Nothing And eventRange.Address <> eventFirstAddress 
End If
Dim eventWS作为工作表
设置eventWS=工作表(“事件表”)
将eventRange设置为范围
设置eventRange=eventWS.Columns(“A:A”).Find(240,xlValues,xlother)
如果不是eventRange,那么就什么都不是了
Dim eventFirstAddress作为字符串
eventFirstAddress=eventRange.Address
做
如果eventWS.Range(“L”和eventRange.Row).Value=busId,则
如果通勤者=真,则
计数=计数+受影响(eventWS.Range(“Q”和eventRange.Row).Value)
其他的
计数=计数+1
如果结束
如果结束
MsgBox(“调用前移动下一步:&eventRange.Row”)
设置eventRange=eventWS.Columns(“A:A”).FindNext(eventRange)
MsgBox(“调用后移动下一步:&eventRange.Row”)
循环而非eventRange为Nothing和eventRange.Address eventFirstAddress
如果结束
Infected()是一个我可以调用以进行内部处理的函数。如果我删除了这个“Count=Count+impacted(..)”,代码运行良好。如果我添加它,“Loop While”将抛出错误91。如果我在移动eventRange之前和之后添加了一个消息框来打印行号,结果显示“MsgBox(“调用后移动下一步:&eventRange.row)”将抛出错误91

因此,我现在不知道问题是由内部函数还是eventRange引起的。希望有人能指出我的错误。多谢各位

以下是内部功能的代码:

Function Affected(markerId As Integer) As Integer

'initialized return value'
AffectedCoummters = 0

'get total financial sheets'
Dim totalFinancial As Integer
totalFinancial = 0
For Each ws In Worksheets
    If InStr(ws.Name, "Financial") > 0 Then
        totalFinancial = totalFinancial + 1
    End If
Next

Dim i As Integer
'run through all financial sheets'
For i = 1 To totalFinancial

    'get current financial sheet'
    Dim financialWS As Worksheet
    Set financialWS = Worksheets("Financial Sheet" & i)

    'get total rows of current operation sheet'
    Dim rowSize As Long
    rowSize = financialWS.Range("A" & financialWS.Rows.Count).End(xlUp).Row

    'if reach the maximum number of rows, the value will be 1'
    'reInitialize rowSize based on version of Excel'
    If rowSize = 1 Then
        If Application.Version = "12.0" Then
            'MsgBox ("You are using Excel 2007")'
            If InStr(ThisWorkbook.Name, ".xlsx") > 0 Then
                rowSize = 1048576
            Else
                'compatible mode'
                rowSize = 65536
            End If
        ElseIf Application.Version = "11.0" Then
            'MsgBox ("You are using Excel 2003")'
            rowSize = 65536
        End If
    End If

    'filter by marker id first inside current financial sheet'
    Dim findMarker As Range
    Set findMarker = financialWS.Columns("K:K").Find(markerId, , xlValues, xlWhole)

    'if found any given marker id'
    If Not findMarker Is Nothing Then

        Dim firstAddress As String
        firstAddress = findMarker.Address

        'check all matched marker id'
        Do

                    AffectedCommuters = AffectedCommuters + financialWS.Range("O" & findMarker.Row).Value

            'move to next'
            Set findMarker = financialWS.Columns("K:K").FindNext(findMarker)

        Loop While Not findMarker Is Nothing And findMarker.Address <> firstAddress

    End If

Next i

End Function
受影响的函数(markerId为整数)为整数
“已初始化返回值”
受影响的客户=0
“获取全部财务报表”
Dim totalFinancial作为整数
totalFinancial=0
对于工作表中的每个ws
如果InStr(ws.Name,“财务”)>0,则
totalFinancial=totalFinancial+1
如果结束
下一个
作为整数的Dim i
“浏览所有财务报表”
对于i=1到totalFinancial
“获取当前财务报表”
Dim financialWS As工作表
设置financialWS=工作表(“财务表”&i)
'获取当前操作表的总行数'
暗行大小与长行大小相同
rowSize=financialWS.Range(“A”&financialWS.Rows.Count).End(xlUp).Row
'如果达到最大行数,该值将为1'
'根据Excel版本重新初始化行大小'
如果rowSize=1,则
如果Application.Version=“12.0”,则
'MsgBox(“您正在使用Excel 2007”)'
如果InStr(thiswoolk.Name,“.xlsx”)>0,则
行大小=1048576
其他的
“兼容模式”
行大小=65536
如果结束
ElseIf Application.Version=“11.0”则
'MsgBox(“您正在使用Excel 2003”)'
行大小=65536
如果结束
如果结束
'首先在当前财务表中按标记id筛选'
暗鳍标记器作为射程
Set findMarker=financialWS.Columns(“K:K”).Find(markerId,xlValues,xlWhole)
'如果找到任何给定的标记id'
如果找不到Marker什么都不是
将第一个地址设置为字符串
firstAddress=findMarker.Address
'检查所有匹配的标记id'
做
影响的通勤者=影响的通勤者+financialWS.Range(“O”和findMarker.Row).值
“转到下一个”
Set findMarker=financialWS.Columns(“K:K”).FindNext(findMarker)
非findMarker时循环为Nothing,findMarker.Address firstAddress
如果结束
接下来我
端函数

对不起,我没有足够的代表发表评论,所以我必须在这里回答:( 我只想说,虽然这是标准的程序使用

 Loop While Not eventRange Is Nothing And eventRange.Address <> eventFirstAddress 
非eventRange时循环为Nothing和eventRange.Address eventFirstAddress
在这种类型的过程中,如果eventRange实际上为Nothing,则该行将抛出错误91,因为eventRange.address不存在。这意味着一旦找到某个内容,就不能修改该行,从而使用.findnext无法再次找到该行

退出do…循环后,可以修改范围以适应。。。
也许您想使用数组来保存.find…findnext结果中的所有行,然后在Do…loop之后对它们进行操作( 我只想说,虽然这是标准的程序使用

 Loop While Not eventRange Is Nothing And eventRange.Address <> eventFirstAddress 
非eventRange时循环为Nothing和eventRange.Address eventFirstAddress
在这种类型的过程中,如果eventRange实际上为Nothing,则该行将抛出错误91,因为eventRange.address不存在。这意味着一旦找到某个内容,就不能修改该行,从而使用.findnext无法再次找到该行

退出do…循环后,可以修改范围以适应。。。
也许您想使用数组保存.find…findnext结果中的所有行,然后在Do…loop

之后对它们进行操作。我们也可以看到您的函数吗?在最小值处。定义行和返回结果的行。您是否立即签入函数是否工作正常并按预期返回结果?当然,KazJaw。我发布了影响d函数。我在其他情况下使用过它,它工作得很好。你的代码中没有任何异常。只要检查
eventWS
是否应该是它。我建议用F8调试一次迭代。再次感谢你,KazJaw。这就是我感到困惑的原因,它应该可以工作!是的,可能是…如果不是,你可以设置附加的条件语句
eventRange为Nothing,则在find next之后
,如果找不到,则退出Do。然后更改您的
循环
条件,该条件似乎无法正常工作。我们也可以看到您的函数吗?最小值。定义行和返回结果的行。如果