VBA -highlighting rows when a condition is met

Evie76

New Member
Joined
Jan 17, 2022
Messages
36
Office Version
  1. 365
Platform
  1. Windows
Morning

I have a code that highlights rows when in column B the word red is inputted, however, I need the code to work when I press enter is that possible

VBA Code:
Sub HighlightRowsWithRedInColumnB()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
    
    ' Set the active sheet
    Set ws = ActiveSheet
    
    ' Find the last row with data in column B
    lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
    
    ' Loop through each row
    For i = 2 To lastRow ' Start from row 2 assuming headers are in row 1
        If InStr(1, ws.Cells(i, "B").Value, "red", vbTextCompare) > 0 Then
            ' Highlight the entire row from column A to O
            ws.Rows(i).Range("A1:O1").Interior.Color = RGB(255, 0, 0) ' Red color
        End If
    Next i
End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Your code is in standard module.
You can add in the Sheet module the code, which is fired after change in a cell occurs (if you just press enter in an empty cell, excel also "thinks" the content has changed):

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Columns("B:B")) Is Nothing Then
  Call HighlightRowsWithRedInColumnB
End If
End Sub

This code reacts only if the change (enter pressed) was in column B (where the main code checks for "red" presence. If you want to react on enter pressed anywhere - just remove "IF Not ... " and "End If" lines

If you want the code to work in eny sheet in workbook insert it in Thisworkbook module, but then the name of sub will be:
VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

Final comment: Your code does not react on "red" removal. So the line which was red, because of "red" in B remains red after "red" is removed from B.
 
Upvote 0
Solution

Forum statistics

Threads
1,223,896
Messages
6,175,263
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top