This article illustrates how to create a search box in Excel for multiple sheets. Assume you have 20 sheets in your workbook. Now you need to search for data in all those sheets relevant to a keyword. It will be very time-consuming and tiresome to do this manually. Fortunately, you can create a search box for your workbook to search through all sheets. Follow the article to learn how to do that.
How to Create a Search Box in Excel for Multiple Sheets: 2 Ways
Assume you have data as follows in multiple sheets. You need to search by names to quickly find all the relevant results in those sheets.
Follow the methods below to be able to do that.
1. Search Multiple Sheets in Excel with Conditional Formatting
Follow the steps below to apply conditional formatting to quickly search through multiple sheets in Excel.
Steps:
- First, select the range B3:F21 in Sheet2. Then, select Conditional Formatting >> New Rule from the Home tab.
- Next, select ‘Use a formula to determine which cells to format’ as the rule type. After that, enter the following formula in the text box below for Rule Description.
=SEARCH(Sheet1!$B$4,$B3&$C3&$D3&$E3&$F3)
- Next, click on Format and choose a Fill color to highlight the cells. Then click OK.
- After that, apply the same formatting to the data ranges in other sheets by following the same procedure.
- Now go to Sheet1 and enter a keyword in cell B4. It is better to use wildcards (*, ?, etc.) in the search box if you don’t need exact matches.
- Then go to Sheet2 to see the following result. You can see the relevant results highlighted.
- Next, go to Sheet3 to see a similar result as follows.
Read More: How to Create Search Box in Excel with Conditional Formatting
2. Create a Search Box in Excel for Multiple Sheets with VBA
Now you might be thinking about getting those highlighted data in the same sheet i.e. Sheet1 where you search for the keyword. Follow the steps below to be able to do that with VBA.
Steps:
- First, make a copy of your dataset, and then press ALT+F11 to open the VBA window. Then select Insert >> Module.
- Next, copy the following code.
Sub SearchMultipleSheets()
Main_Sheet = "VBA"
Search_Cell = "B5"
SearchType_Cell = "C5"
Paste_Cell = "B9"
Searched_Sheets = Array("Dataset 1", "Dataset 2")
Searched_Ranges = Array("B5:F23", "B5:F23")
Copy_Format = True
Last_Row = Sheets(Main_Sheet).Range(Paste_Cell).End(xlDown).Row
Last_Column = Sheets(Main_Sheet).Range(Paste_Cell).End(xlToRight).Column
Set Used_Range = Sheets(Main_Sheet).Range(Cells(Range(Paste_Cell).Row, Range(Paste_Cell).Column), Cells(Last_Row, Last_Column))
Used_Range.ClearContents
Used_Range.ClearFormats
Value1 = Sheets(Main_Sheet).Range(Search_Cell).Value
Count = -1
If Sheets(Main_Sheet).Range(SearchType_Cell).Value = "Case-Sensitive" Then
Case_Sensitive = True
ElseIf Sheets(Main_Sheet).Range(SearchType_Cell).Value = "Case-Insensitive" Then
Case_Sensitive = False
Else
MsgBox ("Choose a Search Type.")
Exit Sub
End If
For S = LBound(Searched_Sheets) To UBound(Searched_Sheets)
Set Rng = Sheets(Searched_Sheets(S)).Range(Searched_Ranges(S))
For i = 1 To Rng.Rows.Count
For j = 1 To Rng.Columns.Count
Value2 = Rng.Cells(i, j).Value
If PartialMatch(Value1, Value2, Case_Sensitive) = True Then
Count = Count + 1
Rng.Rows(i).Copy
Set Paste_Range = Sheets(Main_Sheet).Cells(Range(Paste_Cell).Row + Count, Range(Paste_Cell).Column)
If Copy_Format = True Then
Paste_Range.PasteSpecial Paste:=xlPasteAll
Else
Paste_Range.PasteSpecial Paste:=xlPasteValues
End If
End If
Next j
Next i
Next S
Application.CutCopyMode = False
End Sub
Function PartialMatch(Value1, Value2, Case_Sensitive)
Matched = False
For i = 1 To Len(Value2)
If Case_Sensitive = True Then
If Mid(Value2, i, Len(Value1)) = Value1 Then
Matched = True
Exit For
End If
Else
If Mid(LCase(Value2), i, Len(Value1)) = LCase(Value1) Then
Matched = True
Exit For
End If
End If
Next i
PartialMatch = Matched
End Function
- After that, paste the code onto the module.
- Next, return to the VBA worksheet and insert a rectangle by selecting Insert > Illustrations > Shapes > Rectangle. After that, change the Fill color and type Search to make it look like a button.
- Now, right-click on the rectangular box, and from the context menu select ‘Assign Macro’.
- Then, select the macro name and click OK.
- Next, type the search keyword in cell A4, select Case-insensitive as the Search type, and then click on the Search.
- After that, you will see all the relevant results that were highlighted in the earlier method are listed here. All the rows associated with the name Smith are now visible in the VBA worksheet.
Read More: Create a Search Box in Excel with VBA
Things to Remember
- You must apply conditional formatting to the data in all sheets. And don’t forget to type the keyword in the exact cell referred to in the conditional formatting.
- Don’t forget to search with wildcards to get all possible matching results if you don’t need exact matches.
- You must make a copy of your dataset before applying the code. Otherwise, you may lose your data.
- You can drag the Search button to move it as required.
You can download the practice workbook from the download button below.
Conclusion
Now you know how to create a search box for multiple sheets in Excel. Have you been able to apply the methods to your datasets? Do you have any further queries or suggestions? Please let us know in the comment section below.
Related Articles
- How to Create a Search Box in Excel
- How to Create a Filtering Search Box for Your Excel Data
- How to Create a Search Box in Excel Without VBA
<< Go Back to Search Box in Excel | Data Management in Excel | Learn Excel
Get FREE Advanced Excel Exercises with Solutions!
Trying to do this…this statement isn’t clear to me. “Now, move the datasets to cell A1 and then save the file as a macro-enabled workbook.” If I have multiple sheets, how do I move datasets to Cell A1? On Which sheet?
Hello Brian,
Thanks for reaching out to us. Well, you can ignore that step I think. Have you tried to run the code on your dataset?
Actually, I’d added the step because VBA was acting a little weird if there was a blank column at the beginning. Maybe because of the .EntireRow statement.
So, why don’t you run the code ignoring that step and see if it works? Thanks.
Regards
Md. Shamim Reza (ExcelDemy Team)
My results don’t show up on the first sheet when following these directions. I copy and pasted all the code. Could it be my variables are off?
Hello Robert,
I’ve checked the code again and it is working fine. Perhaps you haven’t used any wildcards and there was no exact match to the search value. Otherwise, you haven’t used the wildcards properly.
And can you please clarify what you mean by “variables are off”? Thanks.
Regards
Md. Shamim Reza (ExcelDemy Team)
Great code!! Just have a question: My cells have multiple words and it seems the code is only looking in the last word of each cell. Is there a workaround for this? so that is searches every word on the cells?
Thans in advance
Dear BZOIRO,
Yes, you can modify the SearchAll function in the given VBA code to search for each word in the cells separately. I have added the modified code in the Excel file below. This modification splits the search value into words using the Split function and loops through each word to find it in the range. If all words are found, the function merges the found ranges into a single range and thoroughly returns it. If any of the words are not found, the function exits and returns Nothing. Here’s how to use this for your case:
1. Initially, assign Macro “SearchMultipleSheetsIndividually” in the Search command box and click Run.
2. Afterward, type the word you wish to look for. Also, don’t forget to use the Wildcards (*, ? etc) while searching. For instance, if you wish to look for a cell of 3 words and has “Emily” in the middle, type *Emily* and then perform searching. Alternatively, if it happens to be the first word, try Emily*.
Download the Excel file to get the modified VBA code and practice by yourself. I hope it works for you.
https://www.exceldemy.com/wp-content/uploads/2023/01/Search-Box-for-Multiple-Sheets.xlsm
Best Regards,
Yousuf Khan (ExcelDemy Team)
Hi Sir,
we need a help to change the (all.EntireRow.Copy dst) to only copy selected Column (A,C,D,P, etc).
Basically i want show data only specific columns (A,C,D,P, etc). after search.
thank you
Hello YOGESH UTEKAR,
Thank you for your comment. To show the data for specific columns (For example: A,C,D,I,P) instead of the entire row you can use the following code.
I have highlighted the portions where I have made changes.
Best Regards,
Mahfuza Anika Era (ExcelDemy Team)