Forum Discussion
Checking multiple sheets in Excel
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.
- Rodrigo_Jan 29, 2024Iron 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- Grassland79Jan 29, 2024Copper ContributorThanks again, that worked great. One more question if you don't mind. I used the cell R1 for the lookup value since cells A through Q were already used for my data. You have your 'Run' button in cells C and D and you display where data was found in A3 - B8. I can't use those cells since I already have data there. Is there a way to change the location of your 'Run' button to S1 and change where you display the found data to maybe S1 - T6? I can get it to run fine by clicking Run under the Macro Tab but your 'Run' button is a nice feature. Thanks!
- Rodrigo_Jan 30, 2024Iron Contributor
Grassland79
"Is there a way to change the location of your 'Run' button to S1"
> Just right click the 'Run' Button and you will be able to move it by click holding the border of that button and dragging it to cell S1 and you can resize it.
"change where you display the found data to maybe S1 - T6"
> First move the list of sheet names from column A to column S (S1:S6)
> here's the updated macro script: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 R1 in the 2024 sheet searchValue = Worksheets("2024").Range("R1").Value ' Get the list of sheet names Set sheetList = Worksheets("2024").Range("S2:S" & Worksheets("2024").Cells(Rows.Count, "S").End(xlUp).Row) ' Clear the previous cell addresses in column T Worksheets("2024").Range("T2:T" & Worksheets("2024").Cells(Rows.Count, "T").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 SubIt will use your cell R1 for the lookup/search value,
once you move the sheet names from Column A (A3:A8) to Column S (S1:S6),
and for the cell referencing address it will be written in column T.