Excel Custom Coloring Charts by RGB number in cell

waller99

New Member
Joined
Apr 30, 2014
Messages
5
I have some excel sheets that are formatted like the following:


COMPANY | TOTAL | RGB
company1 | 10 | 255,000,000
company2 | 20 | 000,255,000
company3 | 30 | 000,000,255
...


My question is that I would like to have a macro that runs on this basic file and creates a bar graph with the data. Then it utilizes the RGB values in the column to change the specific bar for that row. Is this possible, and if so, can someone point me in the right direction? I know a little VBA but I can't figure out how to do this.

Thanks in advance..

Dustin
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Code:
    ActiveSheet.Shapes.AddChart.Select
    With ActiveChart
     .ChartType = xlPie
     .SetSourceData Source:=Range("Sheet1!$A$3:$B$5")
     .SeriesCollection(1).Select
    End With
    
    ActiveChart.SeriesCollection(1).Points(1).Select
    With Selection.Format.Fill
     .Visible = msoTrue
     .ForeColor.RGB = RGB(255, 0, 0)
     .Transparency = 0
     .Solid
    End With
    ActiveChart.SeriesCollection(1).Points(2).Select
    With Selection.Format.Fill
     .Visible = msoTrue
     .ForeColor.RGB = RGB(0, 255, 0)
     .Transparency = 0
     .Solid
    End With
    ActiveChart.SeriesCollection(1).Points(3).Select
    With Selection.Format.Fill
     .Visible = msoTrue
     .ForeColor.RGB = RGB(0, 0, 255)
     .Transparency = 0
     .Solid
    End With
 
Last edited:
Upvote 0
Code:
    ActiveSheet.Shapes.AddChart.Select
    With ActiveChart
     .ChartType = xlPie
     .SetSourceData Source:=Range("Sheet1!$A$3:$B$5")
     .SeriesCollection(1).Select
     .SeriesCollection(1).Points(1).Select
    End With
    With Selection.Format.Fill
     .Visible = msoTrue
     .ForeColor.RGB = RGB(255, 0, 0)
     .Transparency = 0
     .Solid
    End With
    ActiveChart.SeriesCollection(1).Points(2).Select
    With Selection.Format.Fill
     .Visible = msoTrue
     .ForeColor.RGB = RGB(0, 255, 0)
     .Transparency = 0
     .Solid
    End With
    ActiveChart.SeriesCollection(1).Points(3).Select
    With Selection.Format.Fill
     .Visible = msoTrue
     .ForeColor.RGB = RGB(0, 0, 255)
     .Transparency = 0
     .Solid
    End With

I appreciate the response. The RGB values need to be variables because other rows will have a different RGB number. I need to create a bar chart and color each bar the RGB value in column "C". So for each bar in the chart, use the corresponding RGB value. Kind of hard to explain.
 
Upvote 0
Code:
    nR01 = Sheets("Sheet1").Range("C3").Value
    nG01 = Sheets("Sheet1").Range("D3").Value
    nB01 = Sheets("Sheet1").Range("E3").Value
    nR02 = Sheets("Sheet1").Range("C4").Value
    nG02 = Sheets("Sheet1").Range("D4").Value
    nB02 = Sheets("Sheet1").Range("E4").Value
    nR03 = Sheets("Sheet1").Range("C5").Value
    nG03 = Sheets("Sheet1").Range("D5").Value
    nB03 = Sheets("Sheet1").Range("E5").Value
    
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlColumnClustered
    Range("A3:B5").Select
    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveChart.Parent.Delete
    ActiveSheet.Shapes.AddChart.Select
    With ActiveChart
     .ChartType = xlColumnClustered
     .SetSourceData Source:=Range("Sheet1!$A$3:$B$5")
     .SeriesCollection(1).Select
    End With
    
    ActiveChart.SeriesCollection(1).Points(1).Select
    With Selection.Format.Fill
     .Visible = msoTrue
     .ForeColor.RGB = RGB(nR01, nG01, nB01)
     .Transparency = 0
     .Solid
    End With
    ActiveChart.SeriesCollection(1).Points(2).Select
    With Selection.Format.Fill
     .Visible = msoTrue
     .ForeColor.RGB = RGB(nR02, nG02, nB02)
     .Transparency = 0
     .Solid
    End With
    ActiveChart.SeriesCollection(1).Points(3).Select
    With Selection.Format.Fill
     .Visible = msoTrue
     .ForeColor.RGB = RGB(nR03, nG03, nB03)
     .Transparency = 0
     .Solid
    End With
 
Upvote 0
On the statements
Code:
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlColumnClustered
    Range("A3:B5").Select
    ActiveSheet.ChartObjects("Chart 1").Activate
you could already have a chart there on the page, so just use the last two of four lines (Range... and ActiveSheet...), and name the chart from "Chart 1" to whatever yours is called.
 
Upvote 0
And if you absolutely have to have the color vals in "255,255,255" format in a single cell, look at the Split() function, operate on the comma delimiter and break out the 3 numbers into 3 separate variables for each bar.
 
Upvote 0
I have broken the RGB values out into a separate cells.

COMPANY | TOTAL | R | G | B
company1 | 10 | 255 | 000 | 000
company2 | 20 | 000 | 255 | 000
company3 | 30 | 000 | 000 | 255
...
and so on...
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,051
Members
449,206
Latest member
Healthydogs

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