Question

Q: VBA 'selection.find' error handling

I am running a search in my VBA code using:

Columns("C:C").Select
    Selection.Find(What:=account, after:=ActiveCell, LookIn:= _
        xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False).Activate

If the account is not found it returns a run-time error '91. What would be the best way to handle it.  Can I do an 'If then'?  I need to know what the returned value is when it is not found. Or is an error handler better.  I tried using

On error go to _____: but this did not seem to work.

 

Thanks

 

Answer

A:

sub findif()

set mf=Columns("C").Find(What:=account, after:=range("c1"), LookIn:= _
        xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False)

if not mf is nothing then msgbox "its at " & mf.row

end sub


Don Guillett MVP Excel SalesAid Software *** Email address is removed for privacy ***
Don Guillett, SalesAid Software
dguillett1@gmail.com

Did this solve your problem?

Sorry this didn't help.



 
Question Info

Views: 13,399 Last updated: May 3, 2018 Applies to: