How to Build Search Box in Excel
Table of Contents
Introduction
In this tutorial, you'll learn how to build a fully functional search box in Excel without any coding knowledge. This search box will allow you to find specific items in your data, like searching for chocolate chip cookies in a customer list. We will cover exact matches, partial matches, multiple column searches, and even highlight search terms using conditional formatting.
Step 1: Enable Developer Tab
To start, you need to access the Developer tab in Excel, which provides tools for creating forms and controls.
- Open Excel.
- Go to the File menu.
- Select Options.
- In the Excel Options dialog, select Customize Ribbon.
- In the right panel, check the box for Developer.
- Click OK to enable the Developer tab.
Step 2: Add Basic Exact Match Search Box
Next, you will create a basic search box that returns exact matches.
-
Go to the Developer tab.
-
Click on Insert in the Controls section.
-
Choose a Text Box from the ActiveX Controls.
-
Draw the Text Box on your worksheet.
-
Right-click on the Text Box and select Properties.
-
Change the Name property to "txtSearch" and the LinkedCell property to a cell where you want to display the search term.
-
Close the Properties window.
-
Next, create a button for executing the search.
- Go to the Developer tab.
- Click on Insert and choose a Button (ActiveX Control).
- Draw the Button on your worksheet.
- Right-click the Button, select Properties, change the Caption to "Search," and then close the window.
-
Double-click the Button to open the code editor and enter the following code:
Private Sub CommandButton1_Click() Dim searchTerm As String Dim cell As Range Dim found As Range searchTerm = txtSearch.Text Set found = Nothing For Each cell In Range("A1:A100") ' Adjust the range as needed If cell.Value = searchTerm Then Set found = cell Exit For End If Next cell If Not found Is Nothing Then found.Select Else MsgBox "Item not found." End If End Sub -
Close the code editor and return to the Excel worksheet.
Step 3: Add Partial Match Search Box
Now, let's modify the search box to allow for partial matches.
- Open the code editor for the Button you created earlier.
- Replace the existing code with the following:
Private Sub CommandButton1_Click() Dim searchTerm As String Dim cell As Range Dim found As Range Dim firstAddress As String searchTerm = "*" & txtSearch.Text & "*" Set found = Range("A1:A100").Find(What:=searchTerm, LookIn:=xlValues, LookAt:=xlPart) If Not found Is Nothing Then firstAddress = found.Address Do found.Select Set found = Range("A1:A100").FindNext(found) Loop While Not found Is Nothing And found.Address <> firstAddress Else MsgBox "Item not found." End If End Sub - Close the code editor.
Step 4: Add Multiple Column Search Box
To enhance the search functionality further, you can set up a search that checks multiple columns.
- Open the code editor for the Button again.
- Modify the code to check multiple columns:
Private Sub CommandButton1_Click() Dim searchTerm As String Dim cell As Range Dim found As Range Dim firstAddress As String Dim searchRange As Range searchTerm = "*" & txtSearch.Text & "*" Set searchRange = Range("A1:C100") ' Specify your range here Set found = searchRange.Find(What:=searchTerm, LookIn:=xlValues, LookAt:=xlPart) If Not found Is Nothing Then firstAddress = found.Address Do found.Select Set found = searchRange.FindNext(found) Loop While Not found Is Nothing And found.Address <> firstAddress Else MsgBox "Item not found." End If End Sub - Close the code editor.
Step 5: Use Conditional Formatting to Highlight Partial Matches
Finally, you can highlight the found items to make them stand out.
- Select the range you wish to format (e.g., A1:C100).
- Go to the Home tab, click on Conditional Formatting, then New Rule.
- Choose "Use a formula to determine which cells to format."
- Enter the following formula:
=SEARCH($E$1, A1) ' Assuming E1 is where your search term is - Click Format and choose a highlight color.
- Click OK to apply the formatting.
Conclusion
You have now successfully created an advanced search box in Excel that allows for exact and partial matches across multiple columns. Additionally, you've learned how to highlight search results using conditional formatting.
Next steps include experimenting with the search box on your own data and exploring more complex Excel functionalities to further enhance your data management skills.