I would like to enter a value in G1 and on worksheet change all cells in A2:F500 are…
- User
I would like to enter a value in G1 and on worksheet change all cells in A2:F500 are searched for the existence of the value in any position in the range irrespective of font case. For all values found the cell will be highlighted yellow. Can you please write a VBA code to do this.
- Assistant
Sure! You can use the following VBA code to achieve this:
Private Sub Worksheet_Change(ByVal Target As Range) Dim searchValue As String Dim cell As Range ' Define the search value as the value entered in cell G1 searchValue = UCase(Range("G1").Value) ' Loop through each cell in the range A2:F500 For Each cell In Range("A2:F500") ' Check if the cell value contains the search value (ignoring font case) If InStr(1, UCase(cell.Value), searchValue) > 0 Then ' Highlight the cell with yellow color cell.Interior.Color = RGB(255, 255, 0) Else ' Clear the previous highlighting if the search value is not found cell.Interior.Pattern = xlNone End If Next cell End SubTo add this code to your worksheet, follow these steps:
- Right-click on the sheet tab at the bottom of Excel window and select "View Code". This will open the VBA Editor.
- Copy and paste the above code into the code window.
- Close the VBA Editor.
Now, whenever you change the value in cell G1, the code will automatically search for that value in the range A2:F500 and highlight the cells that contain it, ignoring the font case. If a value is not found, the previous highlighting will be cleared.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.