Cell color fill

rjplante

Well-known Member
Joined
Oct 31, 2008
Messages
574
Office Version
  1. 365
Platform
  1. Windows
I have a spreadsheet that I have conditional formatting set up so that when a user selects a name in column C, that row from column A to column Q is highlighted a particular color. It works pretty slick, but as rows are inserted, deleted, or sorted, the formulas for the conditional formatting seem to get jumbled up and don't function as well. I was wondering if there was a better way to do this via VBA. I have a table (included below) in cells CB188:CF193 which contains the users name (column CB), a color example (CC), and then the values for RGB color code, R-(CD), G-(CE), and B-(CF). I have a macro that will change the color of the rows based on the user selected, and I would have to write additional lines for each user (6 in all). Is there a way to reference the table for the user and RGB color matrix, so that I would not have to specify the cell references for each color I need? Sort of like a VLOOKUP formula in VBA.

The end goal is to move away from conditional formatting and transition to VBA control. When rows are then sorted, inserted, or deleted, the VBA remains intact. If the user name in col C changes, the color for that row changes. Coloration of the row remains when the rows are sorted. If this is not a good idea, and conditional formatting is better, let me know and I will maintain what I have in place now.

Thanks for the support,

Robert

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim NameRange As Range
    Dim NameRow As Long
    Dim ColorGus As Long

NameRow = ActiveCell.Row

ColorGus = RGB(Range("CD193").Value, Range("CE193").Value, Range("CF193").Value)

Set NameRange = Range("C3:C255")

    If Not Application.Intersect(NameRange, Range(Target.Address)) Is Nothing Then
              
        If Range(Target.Address) = "Gus" Then
        
            Range("A" & NameRow & ":Q" & NameRow).Interior.Color = ColorGus
        Else
            Range("A" & NameRow & ":Q" & NameRow).Interior.ColorIndex = 0
        End If

    End If
   
End Sub

TW Schedule Upgrade 01-DEC-2020.xlsm
CBCCCDCECF
181RGB
182Initial Draft:255250150
183Ship Date:215240200
184Final Draft255215215
185Draft SDS200215240
186Product Report255220255
187Fill217217217
188Fred255215255
189Gary255220165
190Jon200245200
191Duke200175230
192Bill255255190
193Gus190240255
Job List
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim Fnd As Range
   
   If Not Application.Intersect(Target, Range("C3:C255")) Is Nothing Then
      Set Fnd = Range("CB18:CB193").Find(Target, , , xlWhole, , , False, , False)
      If Not Fnd Is Nothing Then
         Intersect(Target.EntireRow, Range("A:Q")).Interior.Color = Fnd.Offset(, 1).Interior.Color
      Else
         Intersect(Target.EntireRow, Range("A:Q")).Interior.ColorIndex = xlNone
      End If
   End If
End Sub
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,999
Messages
6,175,886
Members
452,679
Latest member
darryl47nopra

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