I have two subs called FindTempRow and FindBatchRow
Sub FindTempRow()
With Worksheets("TEMPLATES").Range("G:G")
Set t = .Find("Template", LookIn:=xlValues)
If Not t Is Nothing Then
FirstAddress1 = t.Address
Do
Call FindBatchRow
Set t = .FindNext(t)
Loop While Not t Is Nothing And t.Address <> FirstAddress1
End If
End With
End Sub
Sub FindBatchRow()
With Worksheets("DISTRIBUTION LIST").Range("C:C")
Set d = .Find(BatchNo, LookIn:=xlValues)
If Not d Is Nothing Then
FirstAddress2 = d.Address
Do
Set d = .FindNext(d)
Loop While Not d Is Nothing And d.Address <> FirstAddress2
End If
End With
End Sub
FindTempRow is calling FindBatchRow inside a Do While Loop.
The problem is whenever I run the code it gives me an error: Runtime Error Code (91) Object Variable or With Block variable not set
The code that gives an error is located in FindTempRow:
Loop While Not t Is Nothing And t.Address <> FirstAddress1
I tried removing call FindBatchRow in Sub FindTempRow and it runs fine. It seems my code is forgetting the address value of t whenever another find method is called in sub FindBatchRow
SOLUTION: by @Rory
REPLACE: Set t = .FindNext(t) from Sub FindBatchRow
WITH: Set t = .Find("Template", After:=t, LookIn:=xlValues)
Loop While Not t Is Nothing And t.Address <> FirstAddress1FindNextrepeats the last search. Since you changed the search parameters in yourFindBatchRowroutine, you are actually repeating that search,not the original one. You need to useFindagain and specify the same parameters.FindNextrepeat theFind:Set t = .Find("Template", After:=t, LookIn:=xlValues)