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:

[TABLE="width: 192"]
<colgroup><col width="64" span="3" style="width:48pt"> </colgroup><tbody>[TR]
[TD="width: 64, align: right"]245[/TD]
[TD="width: 64, align: right"]238[/TD]
[TD="width: 64, align: right"]242[/TD]
[/TR]
[TR]
[TD="align: right"]32[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]22[/TD]
[/TR]
[TR]
[TD="align: right"]150[/TD]
[TD="align: right"]150[/TD]
[TD="align: right"]149[/TD]
[/TR]
[TR]
[TD="align: right"]253[/TD]
[TD="align: right"]205[/TD]
[TD="align: right"]203[/TD]
[/TR]
[TR]
[TD="align: right"]255[/TD]
[TD="align: right"]150[/TD]
[TD="align: right"]134[/TD]
[/TR]
[TR]
[TD="align: right"]107[/TD]
[TD="align: right"]161[/TD]
[TD="align: right"]104[/TD]
[/TR]
[TR]
[TD="align: right"]203[/TD]
[TD="align: right"]59[/TD]
[TD="align: right"]28[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
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
RGBColour

<colgroup><col style="width: 25pxpx"><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]

[TD="align: center"]2[/TD]
[TD="align: right"]245[/TD]
[TD="align: right"]238[/TD]
[TD="align: right"]242[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=F5EEF2]#F5EEF2[/URL] , align: right"][/TD]

[TD="align: center"]3[/TD]
[TD="align: right"]32[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]22[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=200416]#200416[/URL] , align: right"][/TD]

[TD="align: center"]4[/TD]
[TD="align: right"]150[/TD]
[TD="align: right"]150[/TD]
[TD="align: right"]149[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=969695]#969695[/URL] , align: right"][/TD]

[TD="align: center"]5[/TD]
[TD="align: right"]253[/TD]
[TD="align: right"]205[/TD]
[TD="align: right"]203[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FDCDCB]#FDCDCB[/URL] , align: right"][/TD]

[TD="align: center"]6[/TD]
[TD="align: right"]255[/TD]
[TD="align: right"]150[/TD]
[TD="align: right"]134[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FF9686]#FF9686[/URL] , align: right"][/TD]

[TD="align: center"]7[/TD]
[TD="align: right"]107[/TD]
[TD="align: right"]161[/TD]
[TD="align: right"]104[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=6BA168]#6BA168[/URL] , align: right"][/TD]

[TD="align: center"]8[/TD]
[TD="align: right"]203[/TD]
[TD="align: right"]59[/TD]
[TD="align: right"]28[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CB3B1C]#CB3B1C[/URL] , align: right"][/TD]

</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,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

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