Creating a Find macro

pdluce

New Member
Joined
Jul 21, 2006
Messages
22
I have over 60 worksheets in my workbook, all with multiple company names on them. I was trying to find a way on my main page that I could type in a company name, click find, and then have it take me to the appropriate worksheet. The Edit>find function works fine for this, but I was trying to find a way to have a easy to use format where on the actual spreadsheet. I tried to create a macro that would take the input from the cell and copy and paste it into the "Edit>Find" but I couldn't make it work. If anyone has any suggestions, it would be great, thank you.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Do you have a master list in that Workbook that you can select a name and then run a Macro which takes you to the other sheet in the same Workbook, that that same name in it?

Or do you want to type the name to find each time [you know it should be entered exactly the same as it is in the other location]?



Sub FindAcrossAll()

Dim What As Variant
Dim LastAddr As String
Dim LastSheet As String
Dim NeverFound As Boolean

NewSearch:
NeverFound = True

What = Application.InputBox("What are you looking for?")

If What = False Then Exit Sub

For Each Sht In Worksheets
Sht.Activate

Set Found = Sht.UsedRange.Find(What)

If Not Found Is Nothing Then ' The value has been found.
FirstAddress = Found.Address
Do

Found.Activate
LastAddr = Found.Address
LastSheet = Sht.Name
NeverFound = False
Msg = "Keep on searching?"
Title = "Continue ?"

Response = MsgBox(Msg, vbYesNo + vbQuestion, Title)

If Response = vbNo Then ' Doesn't want to continue
MsgBox "Search cancelled."
Found.Activate
Exit Sub ' Quit the macro

End If

Set Found = Cells.FindNext(After:=ActiveCell)

If Found.Address = FirstAddress Then Exit Do

Loop

End If
Next Sht

If NeverFound Then ' Nothing found
ln1 = "The string " & Chr(34) & What & Chr(34) & " was not found !" & vbCrLf
ln2 = "Do you want to start a new search?"
Msg = ln1 & ln2

Style = vbYesNo + vbCritical + vbDefaultButton2
Response = MsgBox(Msg, Style, Title)
If Response = vbNo Then
MsgBox ("Search ended")
Exit Sub
Else
GoTo NewSearch
End If
Else

Msg = "Search complete. Do you want to go back to last found ?"
Style = vbYesNo + vbDefaultButton2
Title = "Search Complete"

Response = MsgBox(Msg, Style, Title)

If Response = vbYes Then
Sheets(LastSheet).Select
Range(LastAddr).Select
Exit Sub
Else

Msg = " Do you want to start a new search?"
Title = "What Next ?"
Style = vbYesNo + vbQuestion + vbDefaultButton2
Response = MsgBox(Msg, Style, Title)
If Response = vbNo Then
MsgBox ("Search has ended")
Exit Sub
Else

GoTo NewSearch
End If
End If
End If
End Sub
 
Upvote 0
They are lists of stocks and industries so there is no master list and the sheets are always expanding. I have a main page where I want to be able to type in a company name and then have it jump to the worksheet that that company is located on. It is fine if i have to type it in exactly the same. Any suggestions?
 
Upvote 0
The code I posted will lookup partial data strings or values!
If you have more than one location it will find them all, but you will need to quit to do any thing at that location.

To build another Sub from the one I posted to continue using the current cell selections value as the string to search for rather that type or paste that name again. You can make a copy of the Sub, change its name and change one line of code, to do this. Then you will have two sets of code to search both conditions!

In the code, near the top, look for:

What = Application.InputBox("What are you looking for?")

and change this line to:

What = ActiveCell.Value
or
What = Selection.Value
 
Upvote 0

Forum statistics

Threads
1,224,607
Messages
6,179,871
Members
452,948
Latest member
UsmanAli786

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