Highlight Range(Rows) based on cell value

excelrxk

New Member
Joined
Sep 6, 2017
Messages
5
Hi all,

I would like to to have a VBA to highlight rows (ex. A2:F2 & V2 if Y2 = "YES").
I found a code but it highlights the entire row.

Code:
Sub highlightROW()   Dim cell As Range


   Range(Range("Y2"), Range("Y2").End(xlDown)).Select
   For Each cell In Selection
      If cell = "YES" Then cell.EntireRow.Interior.ColorIndex = 4
   Next cell
End Sub

Unfortunately I am not able to attached *.xlsm file.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi all,

I would like to to have a VBA to highlight rows (ex. A2:F2 & V2 if Y2 = "YES").
I found a code but it highlights the entire row.

Code:
Sub highlightROW()   Dim cell As Range


   Range(Range("Y2"), Range("Y2").End(xlDown)).Select
   For Each cell In Selection
      If cell = "YES" Then cell.EntireRow.Interior.ColorIndex = 4
   Next cell
End Sub

Unfortunately I am not able to attached *.xlsm file.
is Conditional formatting not an option?

if you insist
try this quick and dirty, not tested routine.
Code:
Sub highlightROW()


Dim lrow As Long
Dim i As Long


lrow = Sheet1.Cells(Rows.Count, 25).End(xlUp).Row
For i = 2 To lrow
      
    If Sheet1.Cells(i, 25) = "YES" Then
        With Sheet1
            .Range(.Cells(i, 1), .Cells(i, 6)).Interior.ColorIndex = 4
            .Cells(i, 22).Interior.ColorIndex = 4
        End With
    End If


Next i


End Sub
 
Upvote 0
the only downside is that, if any value in column Y changes it will not remove the highlight.
 
Upvote 0
revision:
Code:
Sub highlightROW()


Dim lrow As Long
Dim i As Long


lrow = Sheet1.Cells(Rows.Count, 25).End(xlUp).Row
For i = 2 To lrow
      
    If Sheet1.Cells(i, 25) = "YES" Then
        With Sheet1
            .Range(.Cells(i, 1), .Cells(i, 6)).Interior.ColorIndex = 4
            .Cells(i, 22).Interior.ColorIndex = 4
        End With
        Else
        With Sheet1
            .Range(.Cells(i, 1), .Cells(i, 6)).Interior.ColorIndex = Null
            .Cells(i, 22).Interior.ColorIndex = Null
         End With
    End If


Next i


End Sub
 
Upvote 0
This is a classic example why users should not cross-post without a link to the other forum(s) like this link does as I have provided a solution on the other forum effectively wasting ttray33y's time providing a solution here :mad:
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,255
Members
448,556
Latest member
peterhess2002

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