converting color values

fraz627

Board Regular
Joined
Apr 26, 2014
Messages
110
Office Version
  1. 2010
Platform
  1. Windows
I looking to via VBA to change the fill color of a shape using the Hex color from a button on a userform,
example
ActiveSheet.Shapes.Range(Array("KEY_1")).Fill.ForeColor.RGB = RGB(255, 255, 0)
also the same would apply to the Text color


Also how would one use a variable for the RGB color, and convert between the two
thanks
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
You can convert RGB values with Hex function. Pretty sure the hex result will reverse the RGB values with the low order byte on the right and the high order byte on the left so you'd need to input the RGB values as BGR. If you're converting values you'll need to concatenate the hex value as in

...Fill.ForeColor.RGB = "&H" & Hex(RGB(255, 150, 0))
 
Upvote 0
You don't need to do any RGB conversions or assign & update variables to color the shape or its text. For that, all you need is code like:
VBA Code:
Private Sub CommandButton1_Click()
With ActiveSheet.Shapes.Range(Array("KEY_1"))
  .TextFrame.Characters(1, .TextFrame.Characters.Count).Font.Color = CommandButton1.ForeColor
  .Fill.ForeColor.RGB = CommandButton1.ForeColor
End With
End Sub
Of course, if you want to assign color values independently, you could assign & update variables, as in:
VBA Code:
Sub DemoRGB()
Dim R As Long, G As Long, B As Long
R = 255: G = 255: B = 0
With ActiveSheet.Shapes.Range(Array("KEY_1"))
  .TextFrame.Characters(1, .TextFrame.Characters.Count).Font.Color = RGB(R, G, B)
  .Fill.ForeColor.RGB = RGB(R, G, B)
End With
End Sub
or:
VBA Code:
Sub DemoClr()
Dim Clr As Long
Clr = RGB(255, 255, 0)
With ActiveSheet.Shapes.Range(Array("KEY_1"))
  .TextFrame.Characters(1, .TextFrame.Characters.Count).Font.Color = Clr
  .Fill.ForeColor = Clr
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,728
Members
452,939
Latest member
WCrawford

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