JenniferMurphy
Well-known Member
- Joined
- Jul 23, 2011
- Messages
- 2,691
- Office Version
- 365
- Platform
- Windows
I need to change the fill color for a column of cells based on RBG values. For example:
[TABLE="class: grid, width: 300"]
<tbody>[TR]
[TD="align: center"]R/C[/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[/TR]
[TR]
[TD="align: center"]1[/TD]
[TD="align: center"]R
[/TD]
[TD="align: center"]G
[/TD]
[TD="align: center"]B
[/TD]
[TD="align: center"]Color
[/TD]
[/TR]
[TR]
[TD="align: center"]2[/TD]
[TD="align: center"]255[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]3[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]255[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]4[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]255[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: center"]5[/TD]
[TD="align: center"]85[/TD]
[TD="align: center"]170[/TD]
[TD="align: center"]255[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
In D2:D5, I'd like to set the fill color based on the RGB values in the cells in A:C of that row.
I couldn't find a worksheet function, so I wrote a UDF based on code from the macro recorder.
Here's the recorder code:
If I run it from the macro list, it works, but only on D2 and only for that one color. So I tried to adapt it. I got this, which gets a #VALUE error on the .Pattern statement.
Is there a way to make this function work?
PS: how come VBA changes the first character of all of the attributes except for .color to upper case?
[TABLE="class: grid, width: 300"]
<tbody>[TR]
[TD="align: center"]R/C[/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[/TR]
[TR]
[TD="align: center"]1[/TD]
[TD="align: center"]R
[/TD]
[TD="align: center"]G
[/TD]
[TD="align: center"]B
[/TD]
[TD="align: center"]Color
[/TD]
[/TR]
[TR]
[TD="align: center"]2[/TD]
[TD="align: center"]255[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]3[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]255[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]4[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]255[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: center"]5[/TD]
[TD="align: center"]85[/TD]
[TD="align: center"]170[/TD]
[TD="align: center"]255[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
In D2:D5, I'd like to set the fill color based on the RGB values in the cells in A:C of that row.
I couldn't find a worksheet function, so I wrote a UDF based on code from the macro recorder.
Here's the recorder code:
Code:
Sub Macro1()
Range("D2").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.color = 5614335
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End Sub
Code:
Function SetRGB(inTarget As Range, R As Byte, G As Byte, B As Byte)
inTarget.Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.color = RGB(R, G, B)
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End Function
PS: how come VBA changes the first character of all of the attributes except for .color to upper case?