In need of help with a search code - Macros Excel 2010

KyleJackMorrison

Board Regular
Joined
Dec 3, 2013
Messages
107
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
Hello I'm in need of help.
Right here is the back story,
I'm currently away with the United Nations and a apart of my job is to search peoples names and registration before they enter the base. At the moment we have to search over 500 peoples names on a sheet of paper...
I've made an excel sheet with their names and registration numbers on.

I've got a code that I saw on the internet but I would like to tweak it but don't know how.
I would much appreciate it if someone could help me do this:
1. When searching a name it could highlight the row to make it more visible but not permanently highlight it.
2. After the search it could reopen the search box again.
3. Maybe to make it more easy, have a button that would link you to the search pop-up.

Just making this more easier because we have different nationalities that have to use this.

Code:
Sub FindAll()
   Dim strFind As String
   Dim wks As Worksheet
   Dim rngFound As Range
   Dim lngItems As Long
   strFind = InputBox(prompt:="Type in a name or registration to search..", Title:="Search Box,")
   If Len(strFind) > 0 Then
      For Each wks In ActiveWorkbook.Worksheets
         If FindIt(wks, strFind, lngItems) = False Then Exit For
      Next wks
   End If
   MsgBox lngItems & "matches found"
End Sub
Function FindIt(wks As Worksheet, strFind As String, lngMatches As Long) As Boolean
   Dim rngFound As Range
   Dim strFirstFind As String
   FindIt = True
   With wks.UsedRange
      Set rngFound = .Find(What:=strFind, LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False)
      If Not rngFound Is Nothing Then
         strFirstFind = rngFound.Address
         Do
            lngMatches = lngMatches + 1
            Application.Goto rngFound, True
            If MsgBox("Is this what you are looking for?", vbYesNo) = vbYes Then
               FindIt = False
               Exit Do
            End If
            Set rngFound = .FindNext(rngFound)
         Loop While rngFound.Address <> strFirstFind
      End If
   End With
End Function

Many thanks,
Kyle
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Do you mean you want the row to be highlighted a certain color? What do you mean not permanently?
If you want the search box to reappear, how would you like the user to be able to exit the macro?
 
Upvote 0
So like when I search a name it will highlight it yellow so its more visible, then if its not that person i want then it un-highlights that person and highlights the next person.
I don't really want anyone to disable macros as the main use of this Excel document is to search. Or if it needs a close button then i would like help on that too.
Thank you
 
Upvote 0
Try this code:
Code:
Sub FindAll()
    Dim strFind As String
    Dim wks As Worksheet
    Dim rngFound As Range
    Dim lngItems As Long
    strFind = InputBox(prompt:="Type in a name or registration to search..", Title:="Search Box,")
    If Len(strFind) > 0 Then
        For Each wks In ActiveWorkbook.Worksheets
            If FindIt(wks, strFind, lngItems) = False Then Exit For
        Next wks
    End If
    MsgBox lngItems & "matches found"
    If MsgBox("Perform another search?", vbYesNo) = vbNo Then
        Exit Sub
    Else
        Call FindAll
    End If
End Sub
Function FindIt(wks As Worksheet, strFind As String, lngMatches As Long) As Boolean
   Dim rngFound As Range
   Dim strFirstFind As String
   FindIt = True
   With wks.UsedRange
      Set rngFound = .Find(What:=strFind, LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False)
      If Not rngFound Is Nothing Then
         strFirstFind = rngFound.Address
         Do
            lngMatches = lngMatches + 1
            Application.Goto rngFound, True
            Selection.EntireRow.Interior.Color = 65535
            If MsgBox("Is this what you are looking for?", vbYesNo) = vbYes Then
               FindIt = False
               Exit Do
            End If
            Set rngFound = .FindNext(rngFound)
         Selection.EntireRow.Interior.Pattern = xlNone
         Loop While rngFound.Address <> strFirstFind
         Selection.EntireRow.Interior.Pattern = xlNone
      End If
   End With
End Function
As for the button, go to the Developer tab on the ribbon > Insert > Button, and drag the rectangle to create a button and assign the macro to it.
If you don't see the developer tab, go to File > Options > Customize Ribbon, and check the 'Developer' box on the right.
 
Upvote 0

Forum statistics

Threads
1,216,030
Messages
6,128,408
Members
449,448
Latest member
Andrew Slatter

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