+ Reply to Thread
Results 1 to 7 of 7

How do I display an error message if Find Macro is unsuccessful

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-16-2008
    Location
    Mansfield, TX
    Posts
    324

    How do I display an error message if Find Macro is unsuccessful

    The code below searches the entire sheet for an email address that the use has inputted into cell E3. How can I make this code show an error message if there are no matches found?


    Sub delete2()
    '
    ' delete2 Macro
    ' Macro recorded 10/29/2008 by Clayton Grove
    '
    
    '
        Range("E3").Select
        Cells.find(What:=Range("E3"), After:=ActiveCell, LookIn:=xlFormulas, LookAt _
            :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False).Activate
         
        Selection.ClearContents
          Range("E3").Select
        Selection.ClearContents
    
    End Sub

    Thanks!
    Clayton Grove

  2. #2
    Registered User
    Join Date
    07-02-2008
    Location
    Fort Worth, TX
    Posts
    99
    Sub delete2()
    '
    ' delete2 Macro
    ' Macro recorded 10/29/2008 by Clayton Grove
    
        'set up error checker
        On Error Goto Delete2Error 
        Range("E3").Select
        Cells.find(What:=Range("E3"), After:=ActiveCell, LookIn:=xlFormulas, LookAt _
            :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False).Activate
         
        Selection.ClearContents
          Range("E3").Select
        Selection.ClearContents
    
        'stops error checker and returns to normal
        On Error goto 0     
        Exit Sub       'exits sub or Delete2Error will run anyway
    'if error found, comes here
    Delete2Error: 
        msgbox "There was an error"
    End Sub
    Use above and modify to your hearts content.

    Bob's is better as it does more of what you want.
    Last edited by Tirren; 10-29-2008 at 06:49 PM. Reason: Better answer provided.

  3. #3
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284
    Sub delete2()
    '
    ' delete2 Macro
    ' Macro recorded 10/29/2008 by Clayton Grove
    '
    
    '
        Set cell = Cells.Find(What:=Range("E3").Value, _
                            After:=Range("E3"), _
                            LookIn:=xlFormulas, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False, _
                            SearchFormat:=False)
        If cell Is Nothing Then
        
            MsgBox "Nothing found"
        Else
        
            cell.ClearContents
            Range("E3").ClearContents
        End If
        
    End Sub

  4. #4
    Forum Contributor
    Join Date
    02-16-2008
    Location
    Mansfield, TX
    Posts
    324
    Hmm, its not working. I could have done something wrong, but when I put random text in cell e3 and run the macro, I do not get an error.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Maybe ...
    Sub delete2()
        Dim cell    As Range
    
        Set cell = Cells.Find(What:=Range("E3").Value, _
                              After:=Range("E3"), _
                              LookIn:=xlFormulas, _
                              LookAt:=xlWhole, _
                              SearchOrder:=xlByRows, _
                              SearchDirection:=xlNext, _
                              MatchCase:=False, _
                              SearchFormat:=False)
        If cell.Address(0, 0) <> "E3" Then
            MsgBox "Nothing (else) found"
        Else
            cell.ClearContents
            Range("E3").ClearContents
        End If
    End Sub
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Forum Contributor
    Join Date
    02-16-2008
    Location
    Mansfield, TX
    Posts
    324
    Apparently I am rather dense as I have tried all three and none of them display an error message if there is any text in cell E3 if it is blank, they all work dandy. I have even removed all of the address from the sheet to test it out so I know it is not finding something. I am at a loss now.

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Sorry. Change
    If cell.Address(0, 0) <> "E3" Then
    to
    If cell.Address(0, 0) = "E3" Then

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1