Method 1 – Searching Multiple Sheets in Excel Using Conditional Formatting
Follow these steps to apply conditional formatting and quickly search through multiple sheets in Excel:
- Select the range B3:F21 in Sheet2.
- Navigate to the Conditional Formatting option and choose New Rule from the Home tab.
- In the New Formatting Rule dialog, select Use a formula to determine which cells to format.
- Enter the following formula in the text box provided for Rule Description:
=SEARCH(Sheet1!$B$4,$B3&$C3&$D3&$E3&$F3)
- Click on Format and select a fill color to highlight the relevant cells.
- Confirm your settings by clicking OK.
- Repeat the same procedure for other data ranges in different sheets.
- Ensure consistency by applying the same formatting rules.
- Go to Sheet1 and enter a keyword in cell B4.
- Consider using wildcards (*, ?, etc.) in the search box if you don’t require exact matches.
- Switch to Sheet2 to see the relevant results highlighted based on your search criteria.
- Check Sheet3 for a similar result.
By following these steps, you’ll have an efficient search box that spans multiple sheets in your Excel workbook.
Read More: How to Create Search Box in Excel with Conditional Formatting
Method 2 – Creating a Search Box in Excel for Multiple Sheets Using VBA
If you want to retrieve the highlighted data in the same sheet (Sheet1) where you’re searching for a keyword, follow these steps using VBA:
- Make a copy of your dataset. This ensures that you won’t modify the original data during the process.
- Press ALT+F11 to open the Visual Basic for Applications (VBA) window.
- In the VBA window, go to Insert > Module. This creates a new module where you can Insert your VBA code.
- Insert or paste the VBA code that will create the search box functionality. You can customize this code based on your specific requirements.
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
- Return to the VBA worksheet.
- Insert a rectangle by selecting Insert, clicking on Illustrations, choosing Shapes and selecting Rectangle.
- Change the Fill color and type Search to make it look like a button.
- Right-click on the rectangular box, and from the context menu, select Assign Macro.
- Choose the macro name and click OK.
- In cell A4, type the search keyword.
- Select Case-insensitive as the search type, and then click Search.
- You will see all the relevant results that were highlighted earlier 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
- Apply conditional formatting to the data in all sheets.
- Type the keyword in the exact cell referred to in the conditional formatting.
- Use wildcards when searching to get all possible matching results if you don’t need exact matches.
- Make a copy of your dataset before applying the code to avoid data loss.
- You can drag the Search button as needed.
Download Practice Workbook
You can download the practice workbook from here:
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 | 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)
Hi! I watched your video and read through your article in regards to how to create a search box utilizing VBA code.
This video was very informative and helped me understand the code well enough to apply it to my own conditions, outside of one area. The line item under PartialMatch “For i = 1 To Len(Value2)” I am getting an Error 13 of mixed match type. I’ve read through some forums and tried to understand the error code, but can’t figure out where inside the code this would trigger an issue with my spreadsheet.
Hello Brandy
Thanks for your wonderful compliment! Your appreciation means a lot to us.
The Error 13 Type Mismatch in VBA typically occurs when you try to perform an operation on incompatible data types. I have reviewed the code and found that Value2 is used to contain cell values when looping through and comparing with Value1. It seems like some of your values contain errors, which is why it is not possible to use the Len function with this value. So, to avoid this type of situation, you can use IsError to check whether the cells contain any errors or not. If not, perform an operation; otherwise, do nothing.
You can use the following structure:
Hopefully, you have found the ideas you were looking for. Good luck.
Regards
Lutfor Rahman Shimanto
Excel & VBA Developer
ExcelDemy
Hello,
Hoping you can assist. I get an error message of “Run-time error ‘9’: Subscript out of range”. When go to debug it comes up with this line as error:
Set Rng = Sheets(Searched_Sheets(S)).Range(Searched_Ranges(S))
Can you please assist? I have multiple data pages to search.
Hello Michelle,
The “Run-time error ‘9’: Subscript out of range” occurs when a specified sheet or range does not exist. Please check the sheet names and ranges in your Searched_Sheets and Searched_Ranges arrays are correctly spelled and match exactly with your Excel workbook.
I made some changes in the code to check whether sheets and ranges exist in your Excel workbook.
Make sure to update this according to your Excel workbook:
Searched_Sheets = Array(“Dataset 1”, “Dataset 2”) # Update it with your sheet name.
Searched_Ranges = Array(“B5:F23”, “B5:F23”) # Update it with your ranges.
Added SheetExists function it will check if a sheet exists before accessing it.
Error Handling will check for the existence of sheets to prevent “Subscript out of range” errors.
Regards
ExcelDemy
Hello,
I have successfully used your code. Is there a way in which if you entered no text into the search box that no results populate? If I do it now it brings up everyone which causes a long lag as I have a lot of data across multiple sheets. Thanks
Hello Michelle,
Yes, you can add a check in the VBA script to see if the search box is empty before proceeding with the search. If the search box is empty, the subroutine can exit early without performing any operations, preventing the script from unnecessarily processing all the data and causing lag.
Here, added IsEmpty() and a simple comparison to an empty string (“”) to check if the Search_Cell is empty before running the rest of the code.
If the search cell is empty, it displays a message box alerting the user and exits the subroutine early with Exit Sub.
Regards
ExcelDemy