How can I color code a graph in Excel using VBA, using the RGB mix? - vba

I would like to use VBA to modify the color in a graph/chart in Excel based on values in 3 cells (correlated to RGB).
For example, cells A1 (Red), A2 (Green), and A3 (Blue) would each have a value that would correspond to a certain color. Based upon those values, the bar graph color would change to whatever the RGB color indicated. 115-20-110 would give me a pink bar color.
I currently have
ActiveChart.SeriesCollection.Interior.Color = RGB(A1, A2, A3)
But I don't really know VBA and it isn't working. Any help is appreciated, and if there is a less obtuse way I would be interested of course.
Thank you

Try:
ActiveChart.SeriesCollection(1).Interior.Color = RGB(Range("A1").Value, _
Range("A2").Value, _
Range("A3").Value)
SeriesCollection represents all of the series on the chart - you need to pick one to color...

Related

get cell fill color using openpyxl

I would like to get the background fill value for a particular cell in a sheet using openpyxl. I thought that the selected answer from this post openpyxl : Is there a way to search an Excel spreadsheet for cells with specific fill color? would help me, but it isn't working properly.
Here is my current code:
wb1 = Workbook()
ws1 = wb1.create_sheet('new sheet', 0)
ws1.cell(1,1).value = 0.5 # cell A1
rule = ColorScaleRule(start_type='num', start_value=0, start_color='FFFFFF',
end_type='num', end_value=1, end_color='FF007F')
ws1.add(range_string='A1', cfRule=rule)
print('rgb_fg: ', ws1.cell(1,1).fill.fgColor.rgb)
print('rgb_bg: ', ws1.cell(1,1).fill.bgColor.rgb)
output:
rgb_fg: 00000000
rgb_bg: 00000000
Since I assigned 0.5 as the value of cell "A1" in ws1, then I would have no reason to think that the rgb of this cell would be 00000000 (black). It should be a light salmon red color, which is what I get if the sheet is saved and I view it.
Note I printed fgColor and bgColor just to cover all my bases (or the ones I can think of)
Perhaps something funky happens when a ColorScaleRule is applied to a cell so its fill value can't be read in the way I expect?
Conditional formatting is not handled the same way as static colors. Background colors from those conditional rules are dynamic and therefore left to only be applied by spreadsheet software, not the library itself. Therefore, you will not be able to determine this value with openpyxl.
The good news is that if you were to save the spreadsheet, you would see that your code is correct and the cell does appear pink.

Excel VBA : Use the color of a shape in an If/Then case

I have created a chart on Excel using macros. Each of the shapes on the chart is filled with a color according to its category. I was wondering if it is possible to use the color of the shapes in an If/Then case to perform different actions, such as displaying the shapes with specific colors only.
For example, something similar to:
If shape.Fill.ForeColor.SchemeColor = 1 Then
shape.delete
I have tried that, but it doesn't seem to do anything to my chart. Does anyone have an idea of how to do it?
Thank you !!
Did you try checking the color with the corresponding RGB value? Since SchemeColor depends on the current color scheme it might be looking for a different color than you'd expect

Background Color based on difference with cell

I am trying to create a VBA code which allows me to allocate background colors to a cell based on its difference with the neigbouring cell to the left.
Allow me to explain:
http://imgur.com/UUfaFRA
When you look at the image above you can see:
C3 is red because it's value is higher than B3.
C4 has no color as it's value is equal to B4
C5 is green because it's value is lower than B5
I tried to put this into a VBA code but don't succeed:
Sheets("x").Range("C3").FormulaR1C1 =
"=IF(RC>RC[-1],Interior.ColorIndex = 3,IF(RC<RC[-1,Interior.ColorIndex= 4))"
another attempt was by recording an IF function:
If Sheets("x").Range("C3") > Sheets("x").Range("D3") Then
Sheets("x").Range("C3").Interior.ColorIndex = 3
I'm hoping someone could point me in the right direction as I'm (clearly) no VBA guru.
If Sheets("x").Range("C3") > Sheets("x").Range("D3") Then
Sheets("x").Range("C3").Interior.ColorIndex = 3
This piece of code works. there were some range issues which originaly resulted in a failure of this code.
Thank for your support!

Assign cell background color to rgb from referenced cells

I'm working in excel on programatically setting a cell's background color to be a specific RGB color that is referenced in nearby cells. Here's what I'm trying to do:
You can see that I manually set the background color to be the RGB values specified in the cells in the same row on the left, as highlighted in the picture. The first purple cell has RGB (148,91,128).
I want to do this in VBA or maybe even conditional formatting if possible. So far I have tried this simple vba line of code to see if it would work:
Range("F1").Interior.Color = RGB(C1, D1, E1)
However this line of code sets the cell to have a black background like this:
If the code had worked correctly, this cell should have been a nearly white color, not black. Any ideas on why this isn't working? Am I making a mistake is the way I reference the cells for RGB?
It would be nice to be able to assign the range "F1:F__" to reference the cells to the left on each row too, not just one cell. Thanks!
Try this:
Range("F1").Interior.Color = RGB(Range("C1"), Range("D1"), Range("E1"))
In your version of code C1, D1, E1 are taken as variables and they have default value 0, so macro assign color equal to RGB(0, 0, 0) which is black.

Format One Cell with ColorScale Based on Range of Cell Values

I need to format a single cell with a gradient color based on the values of a range of cells. Similar to the conditional formatting with a ColorScale, except only applying the formatting to a single cell based on the values of a range of cells.
Most likely this will need to be done in VBA. I can't seem to find a simple way to do this without rewriting a similar script to the built-in ColorScale methods.
Example:
B10 formatted with a color in a scale based upon the values of B10:B40. ColorScale would be type 3 with Low, Mid (50%), and High. But I don't want to format any cells other than B10, just use the values of the range B10:B40
I will then do the same for B11:B41, but only formatting B11, then B12:B42, B13:B43, etc..
All I need is to figure out how to create a ColorScale based on a range of values but only apply the formatting to one of the cells.
Edit:
If someone knows a way to create a color scale script similar to the conditional formatting one built into Excel, that would also be a good fix. If I can just calculate the scale from the values in the range of cells and apply to the one cell with the color grade, that would be a solution.
I think what you are after is a 3-color scale for B10 to B40 (so that B10 acquires a colour based on that cell's ranking in the range) together with a separate rule that applies white formatting (in appearance, no formatting) to B11:B40. The latter rule should be at the top of the list of rules and Stop if True checked.