cellMatrix.net

Spreadsheet Modeling and Related Topics

Get RGB From Embedded Chart Series

Earlier this week I received a workbook from a coworker containing several embedded column charts. The coworker needed to replicate the colors for each bar in each chart. Unfortunately the color shades for each bar were so close to each other we couldn't tell where the colors were represented in the workbook's palette. After some modification of code found at Dave McRitchie's site, we came up with the following to get the RGB for each bar:

Sub Get_Series_RGB()

    Dim Cht As Chart
    Dim Srs As Series
    Dim R As Integer
    Dim G As Integer
    Dim B As Integer
    Dim Rng As Range

    Set Cht = ActiveChart

    Worksheets.Add().Name = "RGB"
    Set Rng = Sheets("RGB").Range("A1")

    Rng.Value = "Series"
    Rng.Offset(0, 1).Value = "Red"
    Rng.Offset(0, 2).Value = "Green"
    Rng.Offset(0, 3).Value = "Blue"

    Set Rng = Rng.Offset(1, 0)

    For Each Srs In Cht.SeriesCollection

        GetRGB Srs.Interior.Color, R, G, B

        Rng.Value = Srs.Name
        Rng.Interior.Color = Srs.Interior.Color
        Rng.Offset(0, 1).Value = R
        Rng.Offset(0, 2).Value = G
        Rng.Offset(0, 3).Value = B

        Set Rng = Rng.Offset(1, 0)

    Next Srs

End Sub

Sub GetRGB(RGB As Long, ByRef Red As Integer, _
           ByRef Green As Integer, ByRef Blue As Integer)
    Red = RGB And 255
    Green = RGB \ 256 And 255
    Blue = RGB \ 256 ^ 2 And 255
End Sub

For example, based on the following chart:

image

The macro produces the following RGB values for each series:

image

Finally, it should be noted that RGB returned above may not always be the actual RGB of the color shown.


Posted on Saturday, July 25, 2009 | Comments (0) | Permalink

Comment Entry Form

User Information

Name: (Required)

E-Mail Address: (Optional)

Location: (Optional)

Web Site Address: (Optional)

Comment Formatting Reference
Add VBA Code:
<pre>some code here</pre>
Linking:
[url]http://www.example.com/[/url]
[url=http://www.example.com/]my site[/url]
Text Formatting
[size=4]Some larger text[/size]
[color=green]Some green text[/color]
[b]Some bold text[/b]
[i]Some italic text[/i]
Enter Comment Below

Personalization Options
Remember my personal information
Notify me of follow-up comments?

Next entry: VBA to Load Named Ranges for Dynamic Charts

Previous entry: Chart vs. Table Dilemma

<< Back to main

Statistics

  • Total Entries - 136
  • Current Viewers - 19

Categories

Entries by Day

Sep - 2010
S M T W T F S
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 1 2

Recent Comments

Syndicate