VBA Search box macro - small correction needed

ttratl

Board Regular
Joined
Dec 21, 2004
Messages
168
Hi Everyone,

I have this excellent code that finds a SearchString in a list, which works great, except:
When it doesn't find the SearchString you have to click on the OK button 3 times to get rid of it.
Ideally one click would be a fix, or - bring back the Message Box for another search.

Code:
Sub myfindV2()
Dim Message, Title, Default, SearchString
Message = "Enter the name you need to find.." & vbNewLine & "(not case sensitive)" ' Set prompt.
Title = "Find ? On all sheets!" ' Set title.
Default = "" ' Set default.
' Display message, title, and default value.
SearchString = InputBox(Message, Title, Default)

Set S = Sheets.Application
For Each S In Application.Sheets
With S.Range("A1:IV65536")
Set F = .Find(SearchString, MatchCase:=False, LookAt:=xlPart, LookIn:=xlValues)
If F Is Nothing Then
MsgBox "I can't see " & SearchString & " in the list!"
        
Else
Location = F.Address
S.Select
Range(Location).Select
Exit For
End If
End With
Next S

End Sub

Any help appreciated as always.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Switch the Exit For and End If around and you should be good.

Code:
Sub myfindV2()
Dim Message, Title, Default, SearchString
Message = "Enter the name you need to find.." & vbNewLine & "(not case sensitive)" ' Set prompt.
Title = "Find ? On all sheets!" ' Set title.
Default = "" ' Set default.
' Display message, title, and default value.
SearchString = InputBox(Message, Title, Default)

Set S = Sheets.Application
For Each S In Application.Sheets
With S.Range("A1:IV65536")
Set F = .Find(SearchString, MatchCase:=False, LookAt:=xlPart, LookIn:=xlValues)
If F Is Nothing Then
MsgBox "I can't see " & SearchString & " in the list!"
        
Else
Location = F.Address
S.Select
Range(Location).Select

End If
Exit For
End With
Next S

End Sub


Edit: To bring up the searchbox again use this code.

Code:
Sub myfindV2()
Dim Message, Title, Default, SearchString
Message = "Enter the name you need to find.." & vbNewLine & "(not case sensitive)" ' Set prompt.
Title = "Find ? On all sheets!" ' Set title.
Default = "" ' Set default.
' Display message, title, and default value.
SearchStart:
SearchString = InputBox(Message, Title, Default)

Set S = Sheets.Application
For Each S In Application.Sheets
With S.Range("A1:IV65536")
Set F = .Find(SearchString, MatchCase:=False, LookAt:=xlPart, LookIn:=xlValues)
If F Is Nothing Then
MsgBox "I can't see " & SearchString & " in the list!"
GoTo SearchStart
Else
Location = F.Address
S.Select
Range(Location).Select

End If
Exit For
End With
Next S

End Sub
 
Last edited:
Upvote 0
Completely perfect - thank you so much. Couple of learning points for me too - excellent.
 
Upvote 0

Forum statistics

Threads
1,216,086
Messages
6,128,734
Members
449,466
Latest member
Peter Juhnke

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