VBA Code - Undo the Highlight Row when Cell is Blank

Darlie247

New Member
Joined
Apr 2, 2022
Messages
13
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi there,

I have a VBA code that is working great for me thus far. I have it set up that if the drop down is selected as Testing, Passed, or Failed, it will highlight the entire row as yellow, green, or red. However, this color formatting will remain for the entire row even if I delete the entry. For example, if I select the cell to say "Testing" by mistake and then I delete this, the entire row will remain as yellow. Is there something I can add to my code so that if the cell is blank, it will change back to "no fill"?

Here is a sample of what my sheet looks like as well as my code:
AGAIN.xlsm
ABCDEFGHIJK
1
2Validation
3Testing
4
5
6
7
8Passed
9
10Testing
11Passed
12
13Testing
14
15Failed
16
17
18
19
Sheet1
Cells with Data Validation
CellAllowCriteria
G3:G19ListTesting, Passed, Failed

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Cell As Range, Addr As String
   With Range("G3", Cells(Rows.Count, "G").End(xlUp))
    Set Cell = .Find("*Testing*", , xlValues, , , , False, , False)
    If Not Cell Is Nothing Then
      Addr = Cell.Address
      Do
        Cell.EntireRow.Interior.ColorIndex = 6
        Set Cell = .FindNext(Cell)
      Loop While Not Cell Is Nothing And Cell.Address <> Addr
    End If
  End With
 
   With Range("G3", Cells(Rows.Count, "G").End(xlUp))
    Set Cell = .Find("*Failed*", , xlValues, , , , False, , False)
    If Not Cell Is Nothing Then
      Addr = Cell.Address
      Do
        Cell.EntireRow.Interior.ColorIndex = 3
        Set Cell = .FindNext(Cell)
      Loop While Not Cell Is Nothing And Cell.Address <> Addr
    End If
  End With
  
     With Range("G3", Cells(Rows.Count, "G").End(xlUp))
    Set Cell = .Find("*Passed*", , xlValues, , , , False, , False)
    If Not Cell Is Nothing Then
      Addr = Cell.Address
      Do
        Cell.EntireRow.Interior.ColorIndex = 4
        Set Cell = .FindNext(Cell)
      Loop While Not Cell Is Nothing And Cell.Address <> Addr
    End If
  End With
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Does something like this work for you...

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Cell As Range
        For Each Cell In Range("G3", Cells(Rows.Count, "G").End(xlUp))
            Select Case Cell.Text
                Case Is = "Testing"
                    Cell.EntireRow.Interior.ColorIndex = 6
                Case Is = "Failed"
                    Cell.EntireRow.Interior.ColorIndex = 3
                Case Is = "Passed"
                    Cell.EntireRow.Interior.ColorIndex = 4
                Case Else
                    Cell.EntireRow.Interior.ColorIndex = 0
            End Select
        Next
End Sub
 
Upvote 0
Add these three lines of code at the beginning of your event macro Worksheet_SelectionChange, they will clear all highlighting and re-ably it where needed:
VBA Code:
Application.EnableEvents = False
Cells.Interior.Pattern = xlNone
Application.EnableEvents = True
 
Upvote 0
Does something like this work for you...

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Cell As Range
        For Each Cell In Range("G3", Cells(Rows.Count, "G").End(xlUp))
            Select Case Cell.Text
                Case Is = "Testing"
                    Cell.EntireRow.Interior.ColorIndex = 6
                Case Is = "Failed"
                    Cell.EntireRow.Interior.ColorIndex = 3
                Case Is = "Passed"
                    Cell.EntireRow.Interior.ColorIndex = 4
                Case Else
                    Cell.EntireRow.Interior.ColorIndex = 0
            End Select
        Next
End Sub
Thanks for making my code MUCH more simpler!! Works wonderfully :)

Add these three lines of code at the beginning of your event macro Worksheet_SelectionChange, they will clear all highlighting and re-ably it where needed:
VBA Code:
Application.EnableEvents = False
Cells.Interior.Pattern = xlNone
Application.EnableEvents = True
Thanks! This one works as well, and I will definitely be using this for other codes I have :)

Cheers!
 
Upvote 0
You're welcome. We were both happy to help. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,892
Messages
6,175,236
Members
452,621
Latest member
Laura_PinksBTHFT

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