Seaching across ALL sheets...

Mattrixdesign

Board Regular
Joined
Mar 6, 2002
Messages
201
I have an XL workbook with 17 sheets, each containing many Part Numbers.

One way I search across sheets is to highlight each tab (using shift or ctrl) then selecting Find and typing in the numbers/letters. This is a bit cumberson and kind actually be quite dodgy (i.e. higlight all sheets, make a change of edit and it edits every sheet highlighted!)

What I want to do is introduce some kind of search box on the front of the sheet, so people can just type in what they want then click search of press return and it jumps to the relevant point in the work book.

Is this possible???
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Like this?

Code:
Sub Test()
    Dim What As String
    Dim sht As Worksheet
    Dim Found As Range
    Dim FirstAddress As String
    Dim Response
    What = InputBox("Search for :")
    If What = "" Then Exit Sub
    For Each sht In Worksheets
        sht.Activate
        Set Found = sht.Cells.Find(What)
        If Not Found Is Nothing Then
            FirstAddress = Found.Address
            Do
                Found.Activate
                Response = MsgBox("Continue?", vbYesNo + vbQuestion)
                If Response = vbNo Then Exit Sub
                Set Found = Cells.FindNext(After:=ActiveCell)
                If Found.Address = FirstAddress Then Exit Do
            Loop
        End If
    Next sht
    MsgBox "Search Ended!"
End Sub
 
Upvote 0
Andrew Poulsom said:
Like this?

Code:
Sub Test()
    Dim What As String
    Dim sht As Worksheet
    Dim Found As Range
    Dim FirstAddress As String
    Dim Response
    What = InputBox("Search for :")
    If What = "" Then Exit Sub
    For Each sht In Worksheets
        sht.Activate
        Set Found = sht.Cells.Find(What)
        If Not Found Is Nothing Then
            FirstAddress = Found.Address
            Do
                Found.Activate
                Response = MsgBox("Continue?", vbYesNo + vbQuestion)
                If Response = vbNo Then Exit Sub
                Set Found = Cells.FindNext(After:=ActiveCell)
                If Found.Address = FirstAddress Then Exit Do
            Loop
        End If
    Next sht
    MsgBox "Search Ended!"
End Sub

great, it works, thanks!
 
Upvote 0
Hi Andrew

Saw this post of yours today and was wondering if it would be possible to modify the search to find part of a word, ie if you type park, it would find park and parker and spark

Thank for your time

Colin
 
Upvote 0
One more question Nate,

Can the routine be limited to the first 12 sheets in the workbook

Cheers

Colin
 
Upvote 0
Hello again, I believe so,

<font face=Courier New><SPAN style="color:darkblue">Sub</SPAN> SrchBk()
<SPAN style="color:darkblue">Dim</SPAN> ws <SPAN style="color:darkblue">As</SPAN> Worksheet, myVar <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">String</SPAN>, val1 <SPAN style="color:darkblue">As</SPAN> Range
<SPAN style="color:darkblue">Dim</SPAN> val2 <SPAN style="color:darkblue">As</SPAN> Range, tmp <SPAN style="color:darkblue">As</SPAN> Range, cnt <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">Long</SPAN>
myVar = InputBox("Please Enter a Search Term.")
<SPAN style="color:darkblue">If</SPAN> myVar = vbNullString <SPAN style="color:darkblue">Then</SPAN> <SPAN style="color:darkblue">Exit</SPAN> <SPAN style="color:darkblue">Sub</SPAN>
<SPAN style="color:darkblue">For</SPAN> <SPAN style="color:darkblue">Each</SPAN> ws <SPAN style="color:darkblue">In</SPAN> Worksheets([transpose(row(1:12))])
    <SPAN style="color:darkblue">Set</SPAN> val1 = ws.Cells.Find(What:=myVar, LookIn:=xlValues, _
    lookat:=xlPart, MatchCase:=False)
    <SPAN style="color:darkblue">If</SPAN> <SPAN style="color:darkblue">Not</SPAN> val1 <SPAN style="color:darkblue">Is</SPAN> <SPAN style="color:darkblue">Nothing</SPAN> <SPAN style="color:darkblue">Then</SPAN>
        cnt = cnt + 1
        Application.Goto val1
        <SPAN style="color:darkblue">Set</SPAN> tmp = val1
again:
        <SPAN style="color:darkblue">If</SPAN> MsgBox("Seach Again?", 4) = vbNo <SPAN style="color:darkblue">Then</SPAN> <SPAN style="color:darkblue">Exit</SPAN> <SPAN style="color:darkblue">Sub</SPAN>
        <SPAN style="color:darkblue">Set</SPAN> val2 = ws.Cells.FindNext(After:=val1)
        <SPAN style="color:darkblue">If</SPAN> val1.Address <> val2.Address And _
            tmp.Address <> val2.Address <SPAN style="color:darkblue">Then</SPAN>
            Application.Goto val2
            <SPAN style="color:darkblue">Set</SPAN> val1 = val2
            <SPAN style="color:darkblue">GoTo</SPAN> again
        <SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">If</SPAN>
    <SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">If</SPAN>
<SPAN style="color:darkblue">Next</SPAN> ws
<SPAN style="color:darkblue">If</SPAN> <SPAN style="color:darkblue">Not</SPAN> <SPAN style="color:darkblue">CBool</SPAN>(cnt) <SPAN style="color:darkblue">Then</SPAN> MsgBox "No Matches Found"
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Sub</SPAN></FONT>
 
Upvote 0

Forum statistics

Threads
1,214,986
Messages
6,122,611
Members
449,090
Latest member
vivek chauhan

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