Forum Discussion
Checking multiple sheets in Excel
Hi there. If I have multiple sheets in excel where sheet1 = 2024, sheet2 = 2023, sheet3 = 2022, etc., is there a way to check prior year sheets for a value entered into a specific cell in 2024. For instance, if I enter ‘Mike Mitchell’ in cell B3 on sheet 2024, is there a way to check all prior year sheets (2023, 2022, etc.) for a match? And if a match is found, can that match be highlighted in the cell where it was found in the older sheets? So, if ‘Mike Mitchell’ is found on sheet 2022, cell D6, that cell would be highlighted. Thanks in advance for any help you can provide!
18 Replies
- Rodrigo_Steel Contributor
Hello Grassland79
Macro will do your query. I've made a sample file based on your requirements and added some suggestions. as you can see on the image below, I've inserted a button in order to run the macro that will look for your lookup value in B1, and since I added your sheet names on the list, it will also highlight in what certain sheet names your lookup value has been found and added a comment for the referencing (column B)
Sub CheckForMatch() Dim ws As Worksheet Dim rng As Range Dim cell As Range Dim searchValue As String Dim sheetList As Range Dim sheetCell As Range ' Get the value from cell B1 in the 2024 sheet searchValue = Worksheets("2024").Range("B1").Value ' Get the list of sheet names Set sheetList = Worksheets("2024").Range("A4:A" & Worksheets("2024").Cells(Rows.Count, "A").End(xlUp).Row) ' Clear the previous cell addresses in column B Worksheets("2024").Range("B4:B" & Worksheets("2024").Cells(Rows.Count, "B").End(xlUp).Row).ClearContents ' Check if the search value is not an empty string If searchValue <> "" Then ' Loop through each worksheet For Each ws In ThisWorkbook.Worksheets ' Clear all cell highlighting in the worksheet applied by this code For Each cell In ws.UsedRange If cell.Interior.Color = RGB(255, 255, 0) Then ' Yellow cell.Interior.ColorIndex = 0 End If Next cell ' Skip the 2024 sheet If ws.Name <> "2024" Then ' Loop through each cell in the worksheet For Each cell In ws.UsedRange ' If the cell's value matches the search value If cell.Value = searchValue Then ' Highlight the cell cell.Interior.Color = RGB(255, 255, 0) ' Yellow ' Highlight the sheet name in the list and add cell address For Each sheetCell In sheetList If sheetCell.Value = ws.Name Then sheetCell.Interior.Color = RGB(255, 255, 0) ' Yellow sheetCell.Offset(0, 1).Value = "Found in " & cell.Address End If Next sheetCell End If Next cell End If Next ws End If End Sub
Note: Unfortunately, if you have some highlighted cells that are the same with the macro (specifically Yellow or RGB (255, 255, 0)), VBA does not provide a straightforward way to distinguish between highlights applied by code and those applied manually. as a workaround, you could use a specific color for the highlights applied by this code, and then modify the code to only clear cells with that specific color.
- Grassland79Copper ContributorThat worked great, thanks again. One question: If I wanted to use a different cell than B1 for the lookup value (say R1) how would I change the macro to do that? I played around with it a bit to try to make that change but since I'm not familiar with macros, I couldn't get it to work. Thanks again in advance for your help!
- Rodrigo_Steel ContributorHello,
Just replace the "B1" with "R1" in the line of code that retrieves the search value/lookup value.
' Get the value from cell R1 in the 2024 sheet
searchValue = Worksheets("2024").Range("R1").Value
- Grassland79Copper Contributor
Rodrigo_ Awesome, thanks so much. I will give that a try tomorrow.
- peiyezhuBronze ContributorI would like to consolidate all sheets to one big master sheet before check the keywords.