find macro excel 2003 vba

kuhn

New Member
Joined
Oct 22, 2010
Messages
28
hello,

could anyone help me with the following problem:

I've got a macro which searches all worksheets of a workbook for a certain value (entered by user).

It works ok, but the problem is that it generates the error message for each worksheet and I just want one at the end of the search.

here's the code:

Code:
Sub zoek()
    Dim FindString As String
    Dim Rng As Range
    FindString = InputBox("vul het dossiernummer in")
    If Trim(FindString) <> "" Then
        For Each sh In ActiveWorkbook.Worksheets
            With sh.Cells
            Set Rng = .Find(What:=FindString, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not Rng Is Nothing Then
                Application.Goto Rng, True
            Else
                MsgBox "klacht niet gevonden"
            End If
        End With
        Next sh
    End If
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Code:
Sub zoek()
    Dim FindString As String
    Dim Rng As Range
    [COLOR="Red"]Dim bFound As Boolean[/COLOR]
    FindString = InputBox("vul het dossiernummer in")
    If Trim(FindString) <> "" Then
        For Each sh In ActiveWorkbook.Worksheets
            With sh.Cells
            Set Rng = .Find(What:=FindString, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not Rng Is Nothing Then
                Application.Goto Rng, True
                [COLOR="Red"]bFound = True[/COLOR]
            End If
        End With
        Next sh
        [COLOR="Red"]If Not bFound Then MsgBox "klacht niet gevonden"[/COLOR]
    End If
End Sub
 
Upvote 0
I'm guessing you do not mean error msg it iis just displaying the msgbox


not clever enough to know the wording to fix this however you have placed the message box stating not found within each loop of the sheet and it needs placing after all the sheets have been checked
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,628
Members
452,933
Latest member
patv

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