RGB value to cell color

afgi

New Member
Joined
Oct 12, 2010
Messages
9
Hi,

can you pls help me with improving my VBA code. the goal is to:
- make excel automatically change cell colors in column D, according to RGB values in column A, B and C
- I want it to be automatic - every time you fill cells in columns A:C, cell in column D will change color based on RGB values
- I do not want it to delate date written in cell in column D

I have managed to make below code, however I need to make it for every row separately.

Code:
Sub color()
       
    Dim R As Integer, G As Integer, B As Integer
    R = ActiveCell.Offset(0, -3).Value
    G = ActiveCell.Offset(0, -2).Value
    B = ActiveCell.Offset(0, -1).Value
    
    Range("d1:d20").Interior.color = RGB(R, G, B)
End Sub

example RGB values:

245238242
32422
150150149
253205203
255150134
107161104
2035928

<colgroup><col width="64" span="3" style="width:48pt"> </colgroup><tbody>
</tbody>
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
If you are changing the vaules of cols A:C manually try this
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Intersect(Target, Range("A:C")) Is Nothing Then Exit Sub
   With Range("D" & Target.Row)
      .Interior.Color = RGB(.Offset(, -3).Value, .Offset(, -2).Value, .Offset(, -1).Value)
   End With
End Sub
Right click on the sheet tab tab you want this to work on, Click View Code, & paste the above into the window that opens up.
 
Upvote 0
What does "it does not work" mean? Please remember that I cannot see your screen & no nothing about your data.
Did you follow my instructions regarding where to place the code?
Is you data in cols A:C like


Excel 2013/2016
ABCD
1RGBColour
2245238242
332422
4150150149
5253205203
6255150134
7107161104
82035928
Sheet1
 
Last edited:
Upvote 0
What does "it does not work" mean? Please remember that I cannot see your screen & no nothing about your data.
Did you follow my instructions regarding where to place the code?
Is you data in cols A:C like

Excel 2013/2016
ABCD
1RGBColour
2245238242
332422
4150150149
5253205203
6255150134
7107161104
82035928

<colgroup><col style="width: 25pxpx"><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1

pls find file under the link: https://drive.google.com/file/d/1yExDs4ZV0hnzOODYBwsoXoLgg1FKyZBM/view?usp=sharing
 
Upvote 0
If you change any of the values in cols A:C then the corresponding cell in col D will change colour.
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,199
Members
449,072
Latest member
DW Draft

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