title: VBA: error 91 Object variable or with block variable not set
body: <p>I faced a weird issue of VBA error 91. I saw many other people have this problem because they didn't use keyword "Set" for object, whereas that is not my case. </p>
  
  <p>Following is my code:</p>
  
  <pre><code>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" &amp; eventRange.Row).Value = busId Then 
                If commuter = True Then 
                   Count = Count + Affected(eventWS.Range("Q" &amp; eventRange.Row).Value)
                Else
                   Count = Count + 1
                End If 
           End If 
           MsgBox("Before call move next: " &amp; eventRange.Row )
           Set eventRange = eventWS.Columns("A:A").FindNext(eventRange)
           MsgBox("After call move next: " &amp; eventRange.Row )
        Loop While Not eventRange Is Nothing And eventRange.Address &lt;&gt; eventFirstAddress 
  End If
  </code></pre>
  
  <p>Affected() is a function I can call to do internal processing. And if I removed this "Count = Count + Affected(....)", the code was working fine. If I added it, "Loop While " would throw error 91. If I added a message box to print out the row number before and after moving eventRange, it turned out that "MsgBox("After call move next: " &amp; eventRange.Row)" would throw error 91. </p>
  
  <p>Hence, I'm confuse whether the issue is caused by the internal function or the eventRange now. Hope someone can point my mistakes out. Thank you very much. </p>
  
  <p>Following are the codes of internal function:</p>
  
  <pre><code>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") &gt; 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" &amp; i)
  
      'get total rows of current operation sheet'
      Dim rowSize As Long
      rowSize = financialWS.Range("A" &amp; 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") &gt; 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" &amp; findMarker.Row).Value
  
              'move to next'
              Set findMarker = financialWS.Columns("K:K").FindNext(findMarker)
  
          Loop While Not findMarker Is Nothing And findMarker.Address &lt;&gt; firstAddress
  
      End If
  
  Next i
  
  End Function
  </code></pre>