VBA Fill rows with color, but skip cells with already certain color fills

cdalgorta

Board Regular
Joined
Jun 5, 2022
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Hi,
I have the below macro that adds fill color grey to my even rows and white to my odd rows.
How can I make it so this macro skips the "cells" that already have a specific color?
For example:
If cell "E4" has color RGB(255, 192, 0) which is the regular orange, then I want that cell to stay as that color and not change to grey like the rest of that row and all other even rows.
Also, I'd need more than just orange.
Yellow = RGB(255, 255, 0)
Light Green = RGB(226, 239, 218)
Light Blue = RGB(221, 235, 247)
Light Orange = RGB(252, 228, 214)
Light Yellow = RGB(255, 242, 204)

Thank you!

Before:
1660603135442.png


After:
1660603105890.png



Sub Color_Rows()

Dim i, count As Long
Dim e As Long

i = 2
count = ActiveSheet.Cells(Rows.count, "A").End(xlUp).Row

e = ActiveSheet.Cells(1, Columns.count).End(xlToLeft).Column


Do While i <= count

Range(Cells(i, 1), Cells(i, e)).Interior.Color = RGB(217, 217, 217)
i = i + 1

If i > count Then
Exit Do
End If

Range(Cells(i, 1), Cells(i, e)).Interior.Color = RGB(255, 255, 255)
i = i + 1

Loop

End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Try
VBA Code:
Sub Color_Rows()

Dim i, count As Long
Dim e As Long

Application.ScreenUpdating = False

i = 2
count = ActiveSheet.Cells(Rows.count, "A").End(xlUp).Row
e = ActiveSheet.Cells(1, Columns.count).End(xlToLeft).Column

Do While i <= count
    For k = 1 To e
        If i Mod 2 = 0 Then
            If Range(Cells(i, k), Cells(i, k)).Interior.Color = RGB(255, 255, 255) Then
                Range(Cells(i, k), Cells(i, k)).Interior.Color = RGB(217, 217, 217)
            End If
        End If
    Next
    i = i + 1
Loop

End Sub
 
Upvote 0
Try
VBA Code:
Sub Color_Rows()

Dim i, count As Long
Dim e As Long

Application.ScreenUpdating = False

i = 2
count = ActiveSheet.Cells(Rows.count, "A").End(xlUp).Row
e = ActiveSheet.Cells(1, Columns.count).End(xlToLeft).Column

Do While i <= count
    For k = 1 To e
        If i Mod 2 = 0 Then
            If Range(Cells(i, k), Cells(i, k)).Interior.Color = RGB(255, 255, 255) Then
                Range(Cells(i, k), Cells(i, k)).Interior.Color = RGB(217, 217, 217)
            End If
        End If
    Next
    i = i + 1
Loop

End Sub
Hi Zot. Thank you.
I guess I should have said that the reason that I mentioned these:
Orange = RGB(255, 192, 0)
Yellow = RGB(255, 255, 0)
Light Green = RGB(226, 239, 218)
Light Blue = RGB(221, 235, 247)
Light Orange = RGB(252, 228, 214)
Light Yellow = RGB(255, 242, 204)

is because those are the only fills that I want to remain. Any cell with any other color, I'd need them to change to either grey or white depending on which row they are in. Would you know how to make that work? Thank you.

Example:
Before:
1660619698026.png


After:
1660619755504.png
 
Upvote 0
I see. Try this:
VBA Code:
Sub Color_Rows()

Dim i, count As Long
Dim e As Long

Application.ScreenUpdating = False

i = 2
count = ActiveSheet.Cells(Rows.count, "A").End(xlUp).Row
e = ActiveSheet.Cells(1, Columns.count).End(xlToLeft).Column

Do While i <= count
    For k = 1 To e
        Select Case Range(Cells(i, k), Cells(i, k)).Interior.Color
            Case RGB(255, 192, 0), RGB(255, 255, 0), RGB(226, 239, 218), RGB(221, 235, 247), RGB(252, 228, 214), RGB(255, 242, 204)
            
            Case Else
                If i Mod 2 = 0 Then
                    Range(Cells(i, k), Cells(i, k)).Interior.Color = RGB(217, 217, 217)
                Else
                    Range(Cells(i, k), Cells(i, k)).Interior.Color = RGB(255, 255, 255)
                End If
        End Select
    Next
    i = i + 1
Loop

End Sub
 
Upvote 0
Solution
I see. Try this:
VBA Code:
Sub Color_Rows()

Dim i, count As Long
Dim e As Long

Application.ScreenUpdating = False

i = 2
count = ActiveSheet.Cells(Rows.count, "A").End(xlUp).Row
e = ActiveSheet.Cells(1, Columns.count).End(xlToLeft).Column

Do While i <= count
    For k = 1 To e
        Select Case Range(Cells(i, k), Cells(i, k)).Interior.Color
            Case RGB(255, 192, 0), RGB(255, 255, 0), RGB(226, 239, 218), RGB(221, 235, 247), RGB(252, 228, 214), RGB(255, 242, 204)
           
            Case Else
                If i Mod 2 = 0 Then
                    Range(Cells(i, k), Cells(i, k)).Interior.Color = RGB(217, 217, 217)
                Else
                    Range(Cells(i, k), Cells(i, k)).Interior.Color = RGB(255, 255, 255)
                End If
        End Select
    Next
    i = i + 1
Loop

End Sub
My goodness. Thank you so much Zot! You have saved me at least 2 hours/week. 🙏🧎🏻‍♂️
Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,198
Members
452,616
Latest member
intern444

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