VBA & Macro Problems

How to Search Multiple Excel Worksheets and Return the Latest Match using VBA

Adam DavisAdam Davis Nov 2, 2026 868 views

Question details

The user needs a method to search through multiple date-named worksheets, starting from the newest, to find a specific reference and return its corresponding comment.

How to Search Multiple Excel Worksheets and Return the Latest Match Using VBA
Product
Excel
Device & OS
not provided
Scenario
Looking up the most recent data or comments associated with a specific reference across a workbook that logs historical data on separate worksheet tabs.
Observed behavior
The user wants to automate the retrieval of the latest matched comment and the name of the worksheet it was found on, rather than manually checking each tab.
Before you start

Before implementing VBA code, ensure your workbook is saved as an Excel Macro-Enabled Workbook (.xlsm) and that macros are enabled in your application's Trust Center settings.

Solution 1Recommended

Implement a Worksheet_Change Event Macro

This solution automates the search process by triggering a VBA script whenever the reference cell changes, scanning from the newest worksheet backwards.

Instead of placing the code in a standard module, using a Worksheet_Change event in the main sheet's code module allows the search to execute automatically immediately after you type or update the search reference.

The logic relies on looping through the worksheets in descending order (from right to left) assuming your newest sheets are added at the end, and utilizing the UsedRange.Find method to locate the matching reference quickly.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.

2
Access the Worksheet Module

In the Project Explorer pane on the left, double-click the sheet where you will input the search reference (e.g., 'Sheet1' or 'Main'). Do not use a standard module.

3
Add the VBA Code

Create a `Private Sub Worksheet_Change(ByVal Target As Range)` event. Use an If statement to ensure the macro only runs when your specific reference cell changes (e.g., `If Not Intersect(Target, Range("A1")) Is Nothing Then`).

4
Loop and Find the Reference

Write a loop that iterates through your date-named worksheets in reverse order, such as `For i = Worksheets.Count To 2 Step -1`. Inside the loop, use `Worksheets(i).UsedRange.Find(Target.Value)` to look for the reference.

5
Output the Results

When a match is found, extract the adjacent comment using `Offset(0, 1).Value` and retrieve the sheet name using `Worksheets(i).Name`. Write these to your designated output cells and use `Exit For` to stop the loop, ensuring you only get the newest match.

Implement a Worksheet_Change Event Macro
Worksheet Ordering: This method assumes your worksheets are ordered chronologically from oldest to newest (left to right). If they are not natively ordered, your VBA code will need to parse the sheet names as dates to determine the chronological order before searching.
Free Microsoft Office alternative

Use WPS Spreadsheet for Powerful VBA and Macro Automation

WPS Office provides an excellent, free alternative for running VBA macros. You can easily manage complex multi-sheet data lookups, use familiar Excel VBA syntax, and enjoy seamless compatibility with .xlsm files without heavy subscription fees.

  1. 1. Open your file in WPS Spreadsheet: Launch WPS Office and open your .xlsm file containing the data.
  2. 2. Access the Developer Tools: Go to the 'Developer' tab on the top ribbon and click 'VB Editor', or simply press Alt + F11.
  3. 3. Add the Event Macro: Double-click your target worksheet in the Project Explorer and paste your Worksheet_Change VBA code to enable the automated search.
  4. 4. Save and Run: Save your document. Type a new reference into your target cell, and WPS Spreadsheet will automatically execute the macro to find the latest match.
Fully supports Microsoft Excel VBA macros and Worksheet_Change events.High compatibility with .xlsx and .xlsm file formats.Lightweight software with a familiar, easy-to-use interface.Free to download and use for complex data automation tasks.
QA img-9

Frequently Asked Questions

Why is my Worksheet_Change event not triggering?

Application events might be temporarily disabled. You can fix this by typing `Application.EnableEvents = True` into the Immediate Window in the VBA editor and pressing Enter. Also, ensure your macro security settings allow VBA code to run.

Can I use standard formulas like VLOOKUP instead of VBA?

Standard formulas like VLOOKUP cannot dynamically search an unknown number of sheets or automatically stop at the 'latest' match based on date-named tabs. To achieve this dynamic, reverse-order lookup, VBA or Power Query is required.

How do I save a workbook that contains VBA macros?

You must save the file as an Excel Macro-Enabled Workbook (.xlsm). If you save it as a standard .xlsx file, Excel will permanently remove all your VBA code.

What if the reference is not found in any worksheet?

Your VBA code should include an `If Not cell Is Nothing Then` check. If the loop completes without finding a match across all sheets, you can program the macro to output a message like 'Reference Not Found' in your output cell.