How to Find Multiple Matches Across Excel Worksheets with VBA
Question details
The user wants to write a VBA macro to search every worksheet for a specific identifier (like a PSID) and return all matching sales values, while avoiding runtime errors associated with uninitialized arrays and missing ranges.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Running a VBA script to aggregate data by searching for a specific value across multiple sheets and appending all matches into an array or output range.
- Observed behavior
- Scripts fail when UBound is called on a non-array value (if only a single Variant is used) or attempt to reference missing ranges, causing errors during the Find loop.
Ensure your workbook is saved in a macro-enabled format (.xlsm) and that you have enabled Developer options to access the VBA editor safely.
Use a Dynamic Array with Proper Loop Exits
Create a macro that searches each worksheet using a dynamic array, ensuring you safely handle ranges that return as 'Nothing' to prevent runtime errors.
When collecting multiple instances across sheets, use the `Find` and `FindNext` methods within a loop. It is crucial to check if the range actually exists using `If Rng Is Nothing Then Exit Do` (or `Exit For` depending on your loop) before attempting to store the value.
Press Alt + F11 to open the VBA Editor, then go to Insert > Module to create a new script area.
Declare a dynamic array (e.g., `Dim Results() As Variant`) to store your matching values, along with your Range and Worksheet variables.
Start a loop through the workbook using `For Each ws In ThisWorkbook.Worksheets` to ensure every sheet is searched.
Use the `Find` method on your target column (e.g., Column A). If a match is found, store its first address (e.g., `FirstAddress = Rng.Address`) to prevent an infinite loop.
Use a `Do...Loop` with `FindNext`. Add the critical line `If Rng Is Nothing Then Exit Do` immediately after the Find operation to safely exit when no more matches exist on that sheet.
Use `ReDim Preserve` to increase the size of your dynamic array and append the matched sales value from the required column.

Store and Output Multiple Columns using a 2D Array
If you need to retrieve multiple columns of data for each match, utilizing a two-dimensional array is the most efficient approach.
Run VBA Macros Seamlessly in WPS Spreadsheet
WPS Spreadsheet provides excellent support for VBA macros, allowing you to write, edit, and run scripts to find multiple matches across worksheets just like you would in Microsoft Excel.
- 1. Download and Install WPS Office: Get the latest version of WPS Office and open your .xlsm file in WPS Spreadsheet.
- 2. Access the Developer Tools: Navigate to the Developer tab on the ribbon and click on 'VBA Editor' to open the script environment.
- 3. Run Your Macro: Paste your cross-worksheet search code, ensure your loops are properly structured, and click Run to aggregate your data.

Frequently Asked Questions
Why do I get a 'Subscript out of range' error when calling UBound?
This error occurs if the dynamic array was never initialized, typically because the Find method didn't locate any matches. To fix this, always include a boolean flag or an error handling check to verify that matches were found before executing the UBound function.
Should I use 'Exit For' or 'Exit Do' when checking 'If Rng Is Nothing'?
The correct exit statement depends on the loop containing your Find operation. If you are searching within a 'Do...Loop' (which is the standard approach for FindNext operations), you must use 'Exit Do'. If you are iterating using a 'For...Next' loop, use 'Exit For'.
How do I prevent an infinite loop when using FindNext in VBA?
When using FindNext, the search will loop back to the beginning of the sheet once it hits the end. To prevent an infinite loop, record the address of the very first match (e.g., `FirstAddress = Rng.Address`). Inside your loop, check if the address of the newly found range matches `FirstAddress`. If it does, exit the loop.




