VBA code to change the color of cell D1 based on that is enter in cells A1,B1, and C1 in RGB format

SharmaAntriksh

New Member
Joined
Nov 8, 2017
Messages
31
Can you please provide a vba Code to change value of cell D1 everytime i change the value of cells A1, B1, and C1 (RGB codes)[TABLE="width: 500"]
<tbody>[TR]
[TD]A1[/TD]
[TD]B1[/TD]
[TD]C1[/TD]
[TD]D1[/TD]
[/TR]
[TR]
[TD]255[/TD]
[TD]0[/TD]
[TD]23[/TD]
[TD]Antriksh[/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]255[/TD]
[TD]32[/TD]
[TD]John
[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I assume you mean for every row in A:C, not just row 1. I changed the cell background color, but you can change it for font if you want to.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range, d As Range
Dim r As Long, g As Long, b As Long
Set d = Intersect(Target, Range("A:C"))
If d Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each c In d
    r = Cells(c.Row, 1): g = Cells(c.Row, 2): b = Cells(c.Row, 3)
    Cells(c.Row, 4).Interior.Color = RGB(r, g, b)
Next
Application.EnableEvents = True
End Sub
 
Upvote 0
Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim xRow As Long
Dim Red As Integer
Dim Blue As Integer
Dim Green As Integer

If Not Intersect(Target, Range("A:C")) Is Nothing Then
    On Error Resume Next
    xRow = Target.Row
    Red = Cells(xRow, 1)
    Green = Cells(xRow, 2)
    Blue = Cells(xRow, 3)
    Cells(xRow, 4).Interior.Color = RGB(Red, Green, Blue)
End If
End Sub
 
Upvote 0
Did you put the code in the worksheet module of the workbook you want this to happen on and save it as a .xlsm file?
 
Upvote 0
Hey Scott, this code is working perfectly for all the files that i have and i also know that this is an event based procedure and will only run when placed in the specific sheet in VBA code window, but this isn't working for a specific file that i have
 
Upvote 0
Your DEC2HEX can be much simpler because DEC2HEX provides a way to pad out with leading zeros.

=DEC2HEX(A2,2)&DEC2HEX(B2,2)&DEC2HEX(C2,2)

I'm not sure why you want this because the values in Column F through H just duplicate A through C, but just change the range in the code:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim c As Range, d As Range
    Dim r As Long, g As Long, b As Long
    
    Set d = Intersect(Target, Range("A2:C" & Rows.Count))
    
    If d Is Nothing Then Exit Sub
        Application.EnableEvents = False
    For Each c In d
        r = Cells(c.Row, 6)
        g = Cells(c.Row, 7)
        b = Cells(c.Row, 8)
        Cells(c.Row, 5).Interior.Color = RGB(r, g, b)
    Next

Application.EnableEvents = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
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