VBA & Macro Problems

Search Previous Excel Worksheets and Highlight Matching Cells with VBA

Tauseeq MagsiTauseeq Magsi Nov 12, 2026 869 views

Question details

Search older worksheets for a value entered on the current-year worksheet, highlight every matching cell, and record each match location.

Search Previous Excel Worksheets and Highlight Matching Cells with VBA
Product
Microsoft Excel VBA macro
Device & OS
not provided
Scenario
The user enters a lookup value on the current-year sheet and needs to check all previous-year worksheets automatically.
Observed behavior
Matching values should be highlighted in each prior worksheet, while the current-year worksheet is excluded and receives a list of matching sheet names and cell addresses.
Before you start

Save the workbook as a macro-enabled file (.xlsm) and make sure macros are enabled when you open it. Update the worksheet name, lookup cell, and results column in the code if your current-year layout differs from the example.

Solution 1Recommended

Run a VBA macro to search prior worksheets and highlight all exact matches

Use this method when your workbook has separate year-based worksheets and you want a repeatable search from the current-year sheet.

The macro reads the lookup value from cell B1 on the current-year worksheet, skips that worksheet during the search, highlights every exact match in older worksheets, and writes each location to column D. It uses a dedicated light-yellow fill color so it does not need to clear existing formatting before each run.

1
Open the Visual Basic Editor

In Excel, press Alt+F11, then select Insert > Module to create a standard VBA module for the workbook.

2
Paste the search macro

Paste this code into the module: Sub SearchPreviousYears() Dim currentWs As Worksheet, ws As Worksheet Dim searchValue As Variant, hit As Range Dim firstAddress As String Dim resultRow As Long Set currentWs = ThisWorkbook.Worksheets("2024") searchValue = currentWs.Range("B1").Value currentWs.Range("D4:D" & currentWs.Rows.Count).ClearContents resultRow = 4 If Len(Trim(CStr(searchValue))) = 0 Then MsgBox "Enter a value in B1 before running the search.", vbExclamation Exit Sub End If For Each ws In ThisWorkbook.Worksheets If ws.Name <> currentWs.Name Then Set hit = ws.UsedRange.Find(What:=searchValue, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False) If Not hit Is Nothing Then firstAddress = hit.Address Do hit.Interior.Color = RGB(255, 242, 204) currentWs.Cells(resultRow, "D").Value = ws.Name & "!" & hit.Address resultRow = resultRow + 1 Set hit = ws.UsedRange.FindNext(hit) Loop While Not hit Is Nothing And hit.Address <> firstAddress End If End If Next ws MsgBox resultRow - 4 & " matching cell(s) found.", vbInformation End Sub

3
Adjust the current-year worksheet reference

Replace "2024" in Set currentWs = ThisWorkbook.Worksheets("2024") with the exact name of your current-year worksheet. Change Range("B1") if the lookup value is entered in another cell, and change column "D" if you want results elsewhere.

4
Run the macro from the worksheet

Return to Excel, press Alt+F8, select SearchPreviousYears, and click Run. To add a button, use Developer > Insert > Button (Form Control), draw it on the current-year worksheet, and assign SearchPreviousYears.

Run a VBA macro to search prior worksheets and highlight all exact matches
Use a dedicated macro highlight color: The macro applies RGB(255, 242, 204). If you later add code to remove old macro highlights, clear only this dedicated color; otherwise, manually highlighted cells using the same color may also be affected.
Work with spreadsheet macros in WPS Office

Search and highlight worksheet matches with WPS Spreadsheets

WPS Spreadsheets provides a familiar worksheet workspace for organizing prior-year data, using Find to locate values, applying cell highlights, and working with compatible macro-enabled Excel files. You can migrate existing workbook layouts and consolidate records into a master sheet when repeated cross-sheet searches become difficult to manage.

  1. 1. Open the workbook in WPS Spreadsheets: Launch WPS Office, choose Spreadsheets, and open the Excel workbook containing the current-year and prior-year worksheets.
  2. 2. Enter the lookup value: Select the current-year worksheet and enter the value to search for in the designated lookup cell, such as B1.
  3. 3. Locate matches in prior-year data: Open each prior-year worksheet and use Home > Find and Replace > Find, then enter the lookup value and select Find All to review matching cells.
  4. 4. Apply a dedicated highlight fill: Select the matching cells, then use Home > Fill Color and choose a dedicated color reserved for search results.
Find values across spreadsheet data and apply fill-color highlights to matching cells.Create a master worksheet to filter and search prior-year records in one place.Open and save Microsoft Excel .xlsx and macro-enabled .xlsm workbook formats.Use a familiar ribbon-based spreadsheet interface for a smooth transition from Excel.
microsoft office alternative - wps office

Frequently Asked Questions

How do I exclude the current worksheet from a VBA search?

Store the current worksheet in a variable, such as currentWs, and place the search code inside If ws.Name <> currentWs.Name Then. This prevents the macro from searching the sheet that contains the lookup value.

Can the macro find more than one matching cell on the same worksheet?

Yes. Use Range.Find to locate the first match, then use FindNext in a loop until the cell address returns to the first match. Each matched cell can be highlighted and added to the results list.

Why should I use LookAt:=xlWhole in the Find method?

LookAt:=xlWhole finds cells whose complete value matches the lookup value. Use LookAt:=xlPart instead only when you want partial matches, such as finding a keyword within longer text.

Where can the macro list the worksheet and cell address for each match?

Write the result to a column on the current-year worksheet, for example currentWs.Cells(resultRow, "D").Value = ws.Name & "!" & hit.Address. The example starts its results at cell D4.

What is the fastest option for searching records from many yearly worksheets?

For frequent searches or large workbooks, combine records into a master worksheet or table with a Year or Source Sheet column. You can then use filters, Find, or a single lookup formula instead of scanning multiple sheets.