Forum Discussion
Grassland79
Jan 25, 2024Copper Contributor
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 i...
Grassland79
Jan 28, 2024Copper Contributor
That 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_
Jan 29, 2024Iron Contributor
Hello,
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
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.- Grassland79Jan 30, 2024Copper ContributorGreat, that worked. Thanks so much for all your help. So, next year when I add sheet year 2025, will I only have to change the "2024" years in the macro to "2025" or is there something else I will need to do? Can I move the search button from sheet 2024 to sheet 2025? Just thinking ahead and wondering what the best approach will be...