PDA

View Full Version : If statement for Cells.Find



Daxton A.
08-27-2008, 07:20 AM
Hi, what I am trying to do is build a Cells.Find statement that shows all the written text alphabetized. The problem that has me stuck is if the text that I lookup is not there I get an error msg.

I figured that I would just put this Cells.Find into an IF statement and it would be simple, but it is not working. The statement is as follows:
If Cells.Find(What:=textToBeFound, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate = False Then
MsgBox ("Not Found")
Else
Cells.Find(What:=textToBeFound, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
End If

Can I get pointed in the right direction?

Bob Phillips
08-27-2008, 08:24 AM
On Error Resume Next
Set rng = Cells.Find(What:=textToBeFound, _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
On Error GoTo 0
If rng Is Nothing Then
MsgBox ("Not Found")
Else
rng.Select
End If

Daxton A.
08-27-2008, 09:42 AM
What is the "0" in

On Error Goto 0

And what does the variable rng mean? And it is a string right?

Forgive my little bit of knowledge.

Bob Phillips
08-27-2008, 09:46 AM
There is a wonderful facility in VBA called help.

Daxton A.
08-27-2008, 09:57 AM
There is a wonderful facility in VBA called help.

My bad. I look for easy answers too often. The program came up with an error using what I was told. Thanx for ur help xld.

Bob Phillips
08-27-2008, 10:03 AM
That is a different question. It does work, what error did you get?