VBA - Coloring Rows Help Please

ExcelMercy

Board Regular
Joined
Aug 11, 2014
Messages
151
Alrighty, I'm trying to backcolor (interior color) certain rows that match certain text. I'm trying to use an array mixed with a case. I'm getting an error... Any ideas?



Rich (BB code):
Dim ColorArray1         As Variant
Dim ColorArray2         As Variant



ColorArray1 = Array("Brown1", "Brown2", "Brown3", "Brown4")
ColorArray2 = Array("Yellow1", "Yellow2", "Yellow3", "Yellow4", "Yellow5", "Yellow6", "Yellow7")

    Dim C As Long, ColorValue As Long
            For C = 2 To Cells(Rows.Count, "A").End(xlUp).Row
                Select Case Cells(C, "D").Value
                    Case ColorArray1:  ColorValue = RGB(148, 138, 34)
                    Case ColorArray2: ColorValue = RGB(255, 255, 0)
                    Case Else:       ColorValue = xlNone
                End Select
            Rows(C).Interior.Color = ColorValue
      Next
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
You are telling the Select Case statement to select a string value, but then you are putting an array into the cases instead of strings. Thankfully you can represent multiple strings in the same case statement:

Code:
Select Case Cells(C, "D").Value
    Case "Brown1", "Brown2", "Brown3", "Brown4"
        ColorValue = RGB(148, 138, 34)
    Case "Yellow1", "Yellow2", "Yellow3", "Yellow4", "Yellow5", "Yellow6", "Yellow7"
        ColorValue = RGB(255, 255, 0)
    Case Else
        ColorValue = xlNone
End Select
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,148
Members
448,552
Latest member
WORKINGWITHNOLEADER

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