Run-Time error '91': Object variable of With block variable not set

J

jammin1911

I've built a simple macro for excel and all is working very nicely
I've googled this error for an hour now and come up with many othe
people with the problem but no solutions...

the code:

Code
-------------------

Cells.Find(What:=txtfindvalue, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate

-------------------


Produces and error: "Run-Time error '91': Object variable of With bloc
variable not set" if "txtfindvalue" is nowhere to be found. If it find
it, everything works nicely, if it doesnt.. i get that error.

Makes sense, but I'm fairly new and don't understand how to trap an
error in VB. All I'd like to do is trap it and produce my own erro
that won't crash the program.. a simple msgbox that will make a warnin
sound and say "String not Found", etc.

If anyone has any experience with this, please let me know - thanks i
advance
 
D

Die_Another_Day

try this:

On error goto ErrorHandler

'Your Code

exit sub

ErrorHandler:
if err.number = 91 then
msgbox "String not found"
'you have several options now. we can return to the previous code, like
this:
err.clear
resume next
'or just finish and exit like this:
exit sub
end if
'there are many things you can try if these don't work out
 
D

Dave Peterson

You can test for the cell by:

dim FoundCell as range
set foundcell = Cells.Find(What:=txtfindvalue, After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

if foundcell is nothing then
'not found
else
foundcell.activate
end if
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top