VBA Error Handling when dealing with Cells.Find

Nyanko

Active Member
Joined
Sep 1, 2005
Messages
437
Hi,

I'm trying to use a find method to look up a variable in a 2000+ list of data. I'm currently using this code :

I need to see if the variable strAdLink1 exists on sheet AdLinks and then to return a Yes/No in the relevant cell on sheet XML Data

Code:
    For a = 3 To lngXMLLastRow
        strAdLink1 = Cells(a, 1).Value
        For b = 1 To lngLnkLastRow
            Sheets("AdLinks").Select
            Range("A1").Select
            x = Cells.Find(What:=strAdLink1, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
            If IsError(x) Then
                strAdLinkFlag = "No"
                Sheets("XMLData").Select
                Cells(a, 3) = strAdLinkFlag
                Exit For
            Else
                strAdLinkFlag = "Yes"
                Sheets("XMLData").Select
                Cells(a, 3) = strAdLinkFlag
                Exit For
            End If
        Next b
    Next a


The code if great at finding the date but as soon as it hits a value that isn't in the AdLinks worksheet then I get the error : Run Time Error 91 : Object Variable or With Blockvariable not set


Any adise would be greatfully recieved
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
How would I use this except substituting UsedRange with a user defined range such as Range(Cells(10,5), Cells(200, 5))
 
Upvote 0
I found a solution,

Set ws = Sheets("ChooseSheet")
With ws
Set rngFound = Range(Cells(10, 5), Cells(200, 5)).Find(What:="Non Performers", LookIn:=xlFormulas)
End With
If Not rngFound Is Nothing Then
'you found the value - do whatever
rngFound.Activate
rnum = ActiveCell.Row
cnum = ActiveCell.Column
Else
' you didn't find the value
End If

If anyone knows (sees) a better way, please post. Humm, now if I use

Set rngFound = Sheets("ChooseSheet").Range(Cells(10, 5), Cells(200, 5)).Find(What:="Non Performers", LookIn:=xlFormulas)

it seems to work. I must have had a typo but didn't see it, sorry
 
Upvote 0
Hi All,

I have a similar requirement but when I used this code, to find the word "ERROR", it is also searching in the formula and taking action. I needed excel to check only in the outcome / result of the formula but not in the formula (contents of the formula). Could anyone please help?

Thank you in Advance,
Syed Anwar
 
Upvote 0

Forum statistics

Threads
1,213,484
Messages
6,113,920
Members
448,533
Latest member
thietbibeboiwasaco

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