On error after "find me"

Claire Jackson

Board Regular
Joined
Jun 30, 2020
Messages
74
Office Version
  1. 2016
Platform
  1. Windows
Hi all, I'm trying to search for a word in excel but if the word isn't found I'm getting a debug so I've tried the following but it isn't working. What am I doing wrong?

Sub FindMe()

On Error GoTo ErrorHandler

Cells.Find(What:=InputBox("Please enter your search criteria", "Search"), _
After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate

ErrorHandler:
MsgBox "Job name not found"

End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi,
try this update to your code & see if does what you want

VBA Code:
Sub FindMe()
    Dim FoundCell   As Range
    Dim Search      As Variant
    
    Do
        Search = InputBox("Please enter your search criteria", "Search")
        'cancel pressed
        If StrPtr(Search) = 0 Then Exit Sub
    Loop Until Len(Search) > 0
    
    Set FoundCell = Cells.Find(What:=Search, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, _
        SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    
    If Not FoundCell Is Nothing Then
    
        With FoundCell
        
        'search value found - do something
        
        End With
        
    Else
        
        MsgBox Search & Chr(10) & "Job name Not found", 48, "Not Found"
        
    End If
    
End Sub

Dave
 
Upvote 0
If find = nothing: then return nothing, not error.
Try this instead:
VBA Code:
Sub FindMe()
Dim f
set f = Cells.Find(What:=InputBox("Please enter your search criteria", "Search"), _
After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False)
f.select
If f is nothing then MsgBox "Job name not found"
End Sub
 
Upvote 0
PERFECT!!! Thank you so much :)
Hi,
try this update to your code & see if does what you want

VBA Code:
Sub FindMe()
    Dim FoundCell   As Range
    Dim Search      As Variant
   
    Do
        Search = InputBox("Please enter your search criteria", "Search")
        'cancel pressed
        If StrPtr(Search) = 0 Then Exit Sub
    Loop Until Len(Search) > 0
   
    Set FoundCell = Cells.Find(What:=Search, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, _
        SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
   
    If Not FoundCell Is Nothing Then
   
        With FoundCell
       
        'search value found - do something
       
        End With
       
    Else
       
        MsgBox Search & Chr(10) & "Job name Not found", 48, "Not Found"
       
    End If
   
End Sub

Dave
Actually, it's not quite right, it doesn't navigate to the word in the spreadsheet any more
 
Upvote 0
Actually, it's not quite right, it doesn't navigate to the word in the spreadsheet any more

I left that bit open for whatever you wanted to do with the code - just add line in bold

Rich (BB code):
Sub FindMe()
    Dim FoundCell   As Range
    Dim Search      As Variant
    
    Do
        Search = InputBox("Please enter your search criteria", "Search")
        'cancel pressed
        If StrPtr(Search) = 0 Then Exit Sub
    Loop Until Len(Search) > 0
    
    Set FoundCell = Cells.Find(What:=Search, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, _
        SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    
    If Not FoundCell Is Nothing Then
    
    
        
        'search value found - do something
        FoundCell.Select
        
        
    Else
        
        MsgBox Search & Chr(10) & "Job name Not found", 48, "Not Found"
        
    End If
    
End Sub

Dave
 
Upvote 0
I left that bit open for whatever you wanted to do with the code - just add line in bold

Rich (BB code):
Sub FindMe()
    Dim FoundCell   As Range
    Dim Search      As Variant
   
    Do
        Search = InputBox("Please enter your search criteria", "Search")
        'cancel pressed
        If StrPtr(Search) = 0 Then Exit Sub
    Loop Until Len(Search) > 0
   
    Set FoundCell = Cells.Find(What:=Search, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, _
        SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
   
    If Not FoundCell Is Nothing Then
   
   
       
        'search value found - do something
        FoundCell.Select
       
       
    Else
       
        MsgBox Search & Chr(10) & "Job name Not found", 48, "Not Found"
       
    End If
   
End Sub

Dave
Whoop, thank you x
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top