Search form

elmo2000

New Member
Joined
Oct 2, 2017
Messages
15
Hi There

This is my first post :-) (And VBA newby)

I found this code (did a little modifying) in this forum for a search in excel, works fine but can you help me on this one:
How can show a message to the user when nothing is found?

I added MsgBox "Niets gevonden" (Nothing found in English) , but this show on every search. Where do I place within this code?

Code:
[COLOR=#000000][FONT=-webkit-standard]Sub myfind()[/FONT][/COLOR][COLOR=#000000][FONT=-webkit-standard]  Dim Message, Title, Default, SearchString[/FONT][/COLOR]
[COLOR=#000000][FONT=-webkit-standard]  Message = "Zoeken op gedetineerde!" ' Set prompt.[/FONT][/COLOR]
[COLOR=#000000][FONT=-webkit-standard]  Title = "Toets een naam in" ' Set title.[/FONT][/COLOR]
[COLOR=#000000][FONT=-webkit-standard]  Default = "B.Oef" ' Set default.[/FONT][/COLOR]
[COLOR=#000000][FONT=-webkit-standard]  ' Display message, title, and default value.[/FONT][/COLOR]
[COLOR=#000000][FONT=-webkit-standard]  SearchString = InputBox(Message, Title, Default)[/FONT][/COLOR]
[COLOR=#000000][FONT=-webkit-standard]
[/FONT][/COLOR]
[COLOR=#000000][FONT=-webkit-standard]  'SearchString = "Rob"[/FONT][/COLOR]
[COLOR=#000000][FONT=-webkit-standard]  Set S = Sheets.Application[/FONT][/COLOR]
[COLOR=#000000][FONT=-webkit-standard]  For Each S In Application.Sheets[/FONT][/COLOR]
[COLOR=#000000][FONT=-webkit-standard]  With S.Range("B1:B400")[/FONT][/COLOR]
[COLOR=#000000][FONT=-webkit-standard]  Set F = .Find(SearchString, MatchCase:=False, LookAt:=xlPart, [/FONT][/COLOR]
[COLOR=#000000][FONT=-webkit-standard]LookIn:=xlValues)[/FONT][/COLOR]
[COLOR=#000000][FONT=-webkit-standard]   If F Is Nothing Then[/FONT][/COLOR]
[COLOR=#000000][FONT=-webkit-standard]  Else[/FONT][/COLOR]
[COLOR=#000000][FONT=-webkit-standard]  Location = F.Address[/FONT][/COLOR]
[COLOR=#000000][FONT=-webkit-standard]  S.Select[/FONT][/COLOR]
[COLOR=#000000][FONT=-webkit-standard]  Range(Location).Select[/FONT][/COLOR]
[COLOR=#000000][FONT=-webkit-standard]  Exit For[/FONT][/COLOR]
[COLOR=#000000][FONT=-webkit-standard]  End If[/FONT][/COLOR]
[COLOR=#000000][FONT=-webkit-standard]  End With[/FONT][/COLOR]
[COLOR=#000000][FONT=-webkit-standard]  Next S[/FONT][/COLOR]
[COLOR=#000000][FONT=-webkit-standard]  End Sub[/FONT][/COLOR]
 
Corrected for rng error and to only test sheet Verdeling, try:
Code:
Sub myFind()

    Dim LR      As Long
    Dim rng     As Range
    Dim strSrch As String
    
    strSrch = InputBox("Zoeken op gedetineerde!", "Toets een naam in", "B.Oef")
    If Len(strSrch) = 0 Then Exit Sub
            
    With Sheets("Verdeling")
        .Select
        LR = .Cells(.Rows.count, 2).End(xlUp).row
        On Error Resume Next
        Set rng = .Cells(1, 2).Resize(LR).find(what:=strSrch, lookat:=xlPart, LookIn:=xlValues, MatchCase:=False)
        On Error GoTo 0
        If Not rng Is Nothing Then
            rng.Select
            Set rng = Nothing
            Exit For
        Else
            MsgBox "Niets gevonden: " & .Name, vbExclamation, "Niets gevonden"
        End If
    End With
    
End Sub
 
Last edited:
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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