Results 1 to 3 of 3
  1. #1
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Northern, California, USA
    Posts
    1,886
    Thanks
    0
    Thanked 0 Times in 0 Posts

    If Not Found (97 SR2)

    I want to run a sub in VBA that runs a search for a pre-determined string. How can I set code to run if the item is NOT found?

    Currently, I'm using the following to search for the string:

    Cells.find(what:=vName, After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
    MatchCase:=False).Select

    But, if the string isn't found, it crashes. Any help would be greatly appreciated! [img]/forums/images/smilies/smile.gif[/img]

    Warmest Regards,
    <IMG SRC=http://www.wopr.com/w3tuserpics/Kel_sig.gif>
    Moderator:<font color=448800> Pix Place, Internet Explorer</font color=448800>
    <small>www.kvisions.com

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: If Not Found (97 SR2)

    Hi DrkRealm,

    Try the following:

    Dim rng As Range
    Set rng = Cells.Find(what:=vName, After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
    MatchCase:=False)
    If rng Is Nothing Then
    MsgBox "No match"
    ' Other actions if not found go here
    Else
    rng.Select
    ' Other actions if found go here
    End If

    Regards,
    Hans

  3. #3
    2 Star Lounger
    Join Date
    Jan 2002
    Location
    Tampico, Tamps, Mexico
    Posts
    118
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: If Not Found (97 SR2)

    maybe this can help you...

    Sub FindTxt()
    '
    Fnd = "*bo*" 'String to search
    '
    '
    For Each c In [B3:C6]
    If c.Value Like Fnd Then
    c.Font.Name = "Times New Roman"
    c.Font.Bold = True
    Else
    c.Font.Name = "Arial"
    c.Font.Bold = False
    End If
    Next
    End Sub

    or something like this...

    Sub FindDate()
    On Error GoTo Bye
    Dim When As Date
    When = InputBox("Introduce la fecha buscada en formato 'dd/mm/yyyy' " & Chr(13) & "Enter Date in 'mm/dd/yyyy' format", "Fecha", Date)
    When = Format(When, "dd/mm/yyyy")
    'Columns("A:A").Select
    Range("A1:C23").Select
    Selection.Find(What:=When, After:=ActiveCell).Activate
    ans = MsgBox("

Posting Permissions

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