Using Find method in For Each Loop - Object Variable or With Block Variable Not Set

BrandynBlaze

New Member
Joined
Sep 20, 2012
Messages
29
Hi There,

I'm having a hard time figuring out why I'm getting the subscript out of range error on the following code.

All I'm trying to do is use the search terms I have stored in the range "SearchTerm" to find any values that are in the range ToSearch. The lines outside of the the For Each Loop is the same idea and it works just fine, but something about the way my For Each loop is working is causing it to throw an error. I set the range of cell to be the first cell of the SearchTerm range to approximate what is happening in the loop.

I've moved some things around to troubleshoot the "Variable Not Set" error, and all I get is a subscript out of range error instead.

Any ideas what might be happening here?

Code:
    Set cell = ActiveSheet.cells(1,1)

    Set WasFound = ToSearch.Find(cell.Value)
    MsgBox WasFound.Text
    cell.clear

    For Each cell In SearchTerm
    
        Set WasFound = ToSearch.Find(cell.Value)
        MsgBox WasFound.Text
                
    Next cell
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
I think it might be the Find that's causing the problems rather than the loop.

For one thing, you are missing a lot of arguments from Find and when you do that the settings default to what was used in the last Find whether performed manually or through code.
 
Upvote 0

Forum statistics

Threads
1,215,352
Messages
6,124,455
Members
449,161
Latest member
NHOJ

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