Assign cell background color to rgb from referenced cells - vba

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.

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.

Adding a background image to a particular cell

I tried to insert the background image using following code:
Pic = "C://Picture/Logo1"
Activesheet.SetBackgroundPicture Pic
This inserts the picture to full sheet but I want to add it to particular cell or a range of cells. Please help
As far as I know excel doesn't support appending the image to a cell (with or without VBA).
The background of a single cell supports colors/gradients/fill-patters, but not the pictures.
It is possible however, to "place" the picture (shape) object with the same width and height right above the cell and make it locked and move around together with the cell if somebody attempts to resize cell widths. I personally wouldn't go that way, too much to code and too much risk of breaking the structure.

Excel: Background of cell not recognised

Hopefully a quickie.
I have a bizarre case of a cell having a background colour, but excel not recognising it as such.
Upon right click > format cell, no fill is shown, no rgb value assigned to this cell. but the cell is coloured and it can be copied only with VBA's .copy or the Paste Specials Keep Source Formatting and Values & Source Formatting
I would really like to know whats going on, as well as how to edit Tim's code here to copy this colour.
Thank you for your time.
Conditional formatting is not recognized as a background color in VBA.
https://msdn.microsoft.com/en-us/library/office/ff822801.aspx

My Excel VBA conditional formatting (background color) isn't updating

I have an application which opens an Excel workbook and in this workbook there is a sheet which has some cells with conditional formatting (background in red when 1, orange when 2 etc ...).
When there are no values in these cells and I then choose a value from a combobox, it should change the background color but it doesn't. However, when I copy and paste a value from another cell (with conditional formatting), the background color is updated and I have the same problem afterwards (background color doesn't update when I change values).
So I want to create a macro in VBA which allows this update when I change values.
N.B. There are protected cells in the workbook but no protection on these cells.
N.B.2: There are 4 cells with the right background color but they are filled before or during Excel opening (it's not my application).
Application.ScreenUpdating = True 'not working
Application.Calculate 'not working too
another weird thing : if i copy/paste all the values (1,2,3,4) from a cell to another + a ctrl + z, then the conditional formatting works fine for all the cells ...
I've found a solution using Cells.ApplyOulineStyles. It works for the first update of the values. I think i will clear the background for each change and call it again.

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

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...