PDA

View Full Version : Solved: Search for an Excel field



akalehzan
03-23-2009, 07:36 PM
Hi All,

I have the following vba code searching for a specific field.
The problem is that if it does not find the field it errors out.
Is there a way to avoid showing the error in vba?:banghead:

Thanks in advance,

Abrahim


sub findtxt()
Worksheets("Sheet1").Activate
Cells.Find(What:="PatientsBirthDate", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(1, -1).Copy

end sub

Simon Lloyd
03-24-2009, 01:12 AM
You just need error handling like this:
Sub findtxt()
Dim rFound As Range
Worksheets("Sheet1").Activate
Set rFound = Cells.Find(What:="PatientsBirthDate", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
On Error GoTo 0
If Not rFound Is Nothing Then
rFound.Offset(1, -1).Copy
'THE REST OF YOUR PASTE CODE HERE
End If
End Sub

If you have posted this question elsewhere please supply the link here!

akalehzan
03-24-2009, 08:29 AM
Good morning Simon,

Thank you so much for the help:clap:

Abrahim

Simon Lloyd
03-24-2009, 08:40 AM
Abrahim, glad i could help :), if your problem has been solved please mark the thread solved by going to Thread Tools and click the appropriate item!