Home » excel » Changing Color of Chart Shape Textbox (Excel VBA)

Changing Color of Chart Shape Textbox (Excel VBA)

Posted by: admin May 14, 2020 Leave a comment

Questions:

Having trouble figuring out how to take the input of a .Font.ColorIndex and using it to change the font color of a chart’s textbox shape.

.Font.ColorIndex returns 48

When I change the color of the text in a macro, it returns this:

        With Selection.ShapeRange.TextFrame2.TextRange.Font.Fill
            .Visible = msoTrue
            .ForeColor.ObjectThemeColor = msoThemeColorBackground1 ' How to set this value from a font color? Can you?
            .ForeColor.TintAndShade = 0
            .ForeColor.Brightness = -0.5
            .Transparency = 0
            .Solid
        End With

Is this possible?

How to&Answers:

You’re mixing different color types. ObjectThemeColor represents a different type of color than what you have as 48.

Looking at Microsoft website The available options are:

Color types

You probably want to convert your ColorIndex to RGB and then insert. You’ll need a custom function, but not hard. Just paste this function somewhere into your Code module.

Function getRGB(C As Long, LetterTYPE As String) As Integer
        Dim R As Long
        Dim G As Long
        Dim B As Long

        R = C Mod 256
        G = C \ 256 Mod 256
        B = C \ 65536 Mod 256

        If UCase(LetterTYPE) = "R" Then
           getRGB = R
        ElseIf UCase(LetterTYPE) = "G" Then
            getRGB = G
        ElseIf UCase(LetterTYPE) = "B" Then
            getRGB = B
        End If
End Function

Then rewrite your original code to include newly created formula:

YourColor = 48
  With Selection.ShapeRange.TextFrame2.TextRange.Font.Fill
            .Visible = msoTrue
            .ForeColor.RGB = RGB(getRGB(YourColor , "R"), getRGB(YourColor , "G"), getRGB(YourColor , "B"))
            .ForeColor.TintAndShade = 0
            .ForeColor.Brightness = -0.5
            .Transparency = 0
            .Solid
        End With