VBA Run Time Error .Find

lc2189

New Member
Joined
Jun 24, 2016
Messages
6
Hi All,

I'm very new to VBA and try to resolve most of my problems through looking at everyone's helpful posts! however, this time I can't seem to figure it out. I am trying to find "TS" in a specific column and filter for those rows if available. If not available, I just want to exit the sub. However, I keep getting a run time error 91 saying "object variable or with block variable not set." I have included my code below and am hoping someone can help :) I'm not sure what happened because it worked at first, but now I'm receiving the error.

Code:
Sub CleanUp()
'
'Clean Up Macro
'
Dim rng As Range
Set rng = Range("AP:AP")
Dim rngFound As Range
Set rngFound = rng.Find("TS")

If rngFound > 0 Then 'error brings me here
    Range("AP:AP").AutoFilter Field:=42, Criteria1:="TS"
    ActiveSheet.AutoFilter.Range.Offset(1, 0).Rows.SpecialCells(xlCellTypeVisible).Delete (xlShiftUp)
    ActiveSheet.AutoFilter.ShowAllData
End If

End Sub

[\code]
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try this to test if rngFound was set to a cell. It wont throw an error.

If Not rngFound Is Nothing Then


Here's a good tutorial on the Find method
TIP: You really should explicitly set the arguments for the .Find command. Otherwise you may not get the result you expect.
 
Last edited:
Upvote 0
Thank you!! That worked perfectly. I updated my code to the below for those looking at this later :)

Code:
Sub CleanUp()
 '
 'Clean Up Macro
 '
 Dim rng As Range
 Set rng = Range("AP:AP")
 Dim rngFound As Range
 Set rngFound = rng.Find(What:="TS", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows)

 If Not rngFound Is Nothing Then
 Range("AP:AP").AutoFilter Field:=42, Criteria1:="TS"
 ActiveSheet.AutoFilter.Range.Offset(1, 0).Rows.SpecialCells(xlCellTypeVisible).Delete (xlShiftUp)
 ActiveSheet.AutoFilter.ShowAllData
 End If

 End Sub

[\code]
 
Upvote 0
The documentation of the range.Find method says
"This method returns Nothing if no match is found"
so change 'If rngFound > 0' to 'if not rngFound is nothing'
 
Upvote 0

Forum statistics

Threads
1,214,385
Messages
6,119,205
Members
448,874
Latest member
Lancelots

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top