Consulting

Results 1 to 4 of 4

Thread: Solved: Search for an Excel field

  1. #1

    Solved: Search for an Excel field

    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?

    Thanks in advance,

    Abrahim

    [VBA]
    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
    [/VBA]

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    You just need error handling like this:
    [VBA]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[/VBA]

    If you have posted this question elsewhere please supply the link here!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3

    Search for an Excel field

    Good morning Simon,

    Thank you so much for the help

    Abrahim

  4. #4
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    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!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •