SOLVED
Home

Colouring a cell with a calculated colour

%3CLINGO-SUB%20id%3D%22lingo-sub-359781%22%20slang%3D%22en-US%22%3EColouring%20a%20cell%20with%20a%20calculated%20colour%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-359781%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3EI%20need%20to%20colour%20a%20cell%20with%20a%20color%20%3CSTRONG%3Ecalculated%3C%2FSTRONG%3E%20from%20its%20RGB%20components%20or%20defined%20by%20its%20hexadecimal%20code.%3C%2FP%3E%3CP%3ESimple%20conditional%20formatting%20seems%20not%20to%20be%20usable%20for%20that%2C%20as%20it%20allows%20only%20a%20choice%20between%20a%20limited%20number%20of%20predefined%20colors.%3C%2FP%3E%3CP%3EI%20haven't%20seen%20any%20information%20on%20this%20anywhere.%3C%2FP%3E%3CP%3EIs%20it%20possible%20and%20how%20could%20I%20make%20it%3F%3C%2FP%3E%3CP%3EI'm%20using%20Excel%20for%20Mac%202011%20version%2014.7.0%20on%20a%20MacBookPro%20under%20macOS%20High%20Sierra%2010.13.6%2C%20but%20I%20think%2Fhope%20that%20my%20question%20is%20a%20generic%20one%20and%20not%20dependant%20on%20the%20environment.%3C%2FP%3E%3CP%3EThanks%20in%20advance%20for%20any%20help%20on%20this%20issue.%3C%2FP%3E%3CP%3EGerard%3C%2FP%3E%3CP%3E--%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-359781%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-359940%22%20slang%3D%22en-US%22%3ERe%3A%20Colouring%20a%20cell%20with%20a%20calculated%20colour%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-359940%22%20slang%3D%22en-US%22%3E%3CP%3EGood%20news%3A%3C%2FP%3E%3CP%3EMy%20tests%20are%20going%20very%20well%3A%20I%20have%20been%20able%20to%20run%20successfully%20the%20Excel%20Macro%3A%3C%2FP%3E%3CDIV%3E%3CEM%3ESub%20Colourise()%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E'%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E'%20Colourise%20Macro%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E'%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E'%20Colours%20all%20selected%20cells%2C%20based%20on%20their%20current%20integer%20rgb%20value%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E'%20For%20e.g.%20(it's%20a%20bit%20backward%20from%20what%20you%20might%20expect)%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E'%20255%20%3D%20%23ff0000%20%3D%20red%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E'%20256*255%20%3D%20%2300ff00%20%3D%20green%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E'%20256*256*255%20%230000ff%20%3D%20blue%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E'%20255%20%2B%20256*256*255%20%23ff00ff%20%3D%20magenta%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E'%20and%20so%20on...%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E'%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E'%20Keyboard%20Shortcut%3A%20Ctrl%2BShift%2BC%20(or%20whatever%20you%20want%20to%20set%20it%20to)%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E'%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3EFor%20Each%20cell%20In%20Selection%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3EIf%20WorksheetFunction.IsNumber(cell)%20Then%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3Ecell.Interior.Color%20%3D%20cell.Value%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3EEnd%20If%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3ENext%20cell%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3EEnd%20Sub%3C%2FEM%3E%3C%2FDIV%3E%3CP%3EBUT%20one%20has%20to%20be%20very%20cautious%20with%20the%20input%20data%20as%20(at%20least%20with%20my%20version)%20Excel%20processes%20the%20colour%20value%20as%20%22BGR%22%20as%20explained%20in%20the%20code%2C%20and%20%3CSTRONG%3Enot%3C%2FSTRONG%3E%20%22RGB%22%20as%20I%20was%20expecting!%3C%2FP%3E%3CP%3E%3CSTRONG%3EThe%20Cell%20content%20has%20to%20be%3A%26nbsp%3B(Blue*256%2BGreen)*256%2B%3C%2FSTRONG%3E%3CSPAN%3E%3CSTRONG%3ERed%3C%2FSTRONG%3E%20%26nbsp%3Bto%20get%20the%20right%20colour!!!%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3EGerard%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E--%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-359887%22%20slang%3D%22en-US%22%3ERe%3A%20Colouring%20a%20cell%20with%20a%20calculated%20colour%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-359887%22%20slang%3D%22en-US%22%3E%3CBLOCKQUOTE%3E%3CHR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F259521%22%20target%3D%22_blank%22%3E%40Rich99%3C%2FA%3E%26nbsp%3Bwrote%3A%3CBR%20%2F%3EHi%2C%20Have%20a%20look%20at%20this%20web%20page%2C%20seems%20to%20have%20what%20your%20looking%20for%2C%20%3CA%20href%3D%22https%3A%2F%2Fstackoverflow.com%2Fquestions%2F1426369%2Fexcel-set-background-colour-of-cell-to-rgb-value-of-data-in-cell%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fstackoverflow.com%2Fquestions%2F1426369%2Fexcel-set-background-colour-of-cell-to-rgb-value-of-data-in-cell%3C%2FA%3E%3CHR%20%2F%3E%3C%2FBLOCKQUOTE%3E%3CP%3E%3CBR%20%2F%3EGreat%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F259521%22%20target%3D%22_blank%22%3ERich99%20%3C%2FA%3E%2C%20%26nbsp%3Bthanks%20a%20lot%2C%20this%20link%20seems%20to%20fit%20exactly%20what%20I%20want%20to%20do.%3C%2FP%3E%3CP%3EMy%20understanding%20is%20that%20this%20Web%20page%20proposes%20two%20different%20methods%2C%20one%20based%20on%20VBA%2C%20the%20other%20one%20based%20on%20Macros.%3C%2FP%3E%3CP%3EI%20am%20not%20familiar%20with%20any%20of%20them%20but%20I%20will%20try%20and%20learn!%3C%2FP%3E%3CP%3EDo%20you%20have%20any%20advice%20about%20which%20one%20to%20try%20first%3F%3C%2FP%3E%3CP%3EBest%2C%3C%2FP%3E%3CP%3EGerard%3C%2FP%3E%3CP%3E--%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-359852%22%20slang%3D%22en-US%22%3ERe%3A%20Colouring%20a%20cell%20with%20a%20calculated%20colour%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-359852%22%20slang%3D%22en-US%22%3EHi%2C%20Have%20a%20look%20at%20this%20web%20page%2C%20seems%20to%20have%20what%20your%20looking%20for%2C%20%3CA%20href%3D%22https%3A%2F%2Fstackoverflow.com%2Fquestions%2F1426369%2Fexcel-set-background-colour-of-cell-to-rgb-value-of-data-in-cell%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fstackoverflow.com%2Fquestions%2F1426369%2Fexcel-set-background-colour-of-cell-to-rgb-value-of-data-in-cell%3C%2FA%3E%3C%2FLINGO-BODY%3E
GFontaine
New Contributor

Hi,

I need to colour a cell with a color calculated from its RGB components or defined by its hexadecimal code.

Simple conditional formatting seems not to be usable for that, as it allows only a choice between a limited number of predefined colors.

I haven't seen any information on this anywhere.

Is it possible and how could I make it?

I'm using Excel for Mac 2011 version 14.7.0 on a MacBookPro under macOS High Sierra 10.13.6, but I think/hope that my question is a generic one and not dependant on the environment.

Thanks in advance for any help on this issue.

Gerard

-- 

3 Replies
Solution
Hi, Have a look at this web page, seems to have what your looking for, https://stackoverflow.com/questions/1426369/excel-set-background-colour-of-cell-to-rgb-value-of-data...

@Rich99 wrote:
Hi, Have a look at this web page, seems to have what your looking for, https://stackoverflow.com/questions/1426369/excel-set-background-colour-of-cell-to-rgb-value-of-data...


Great Rich99 ,  thanks a lot, this link seems to fit exactly what I want to do.

My understanding is that this Web page proposes two different methods, one based on VBA, the other one based on Macros.

I am not familiar with any of them but I will try and learn!

Do you have any advice about which one to try first?

Best,

Gerard

-- 

Good news:

My tests are going very well: I have been able to run successfully the Excel Macro:

Sub Colourise()
'
' Colourise Macro
'
' Colours all selected cells, based on their current integer rgb value
' For e.g. (it's a bit backward from what you might expect)
' 255 = #ff0000 = red
' 256*255 = #00ff00 = green
' 256*256*255 #0000ff = blue
' 255 + 256*256*255 #ff00ff = magenta
' and so on...
'
' Keyboard Shortcut: Ctrl+Shift+C (or whatever you want to set it to)
'
For Each cell In Selection
If WorksheetFunction.IsNumber(cell) Then
cell.Interior.Color = cell.Value
End If
Next cell
End Sub

BUT one has to be very cautious with the input data as (at least with my version) Excel processes the colour value as "BGR" as explained in the code, and not "RGB" as I was expecting!

The Cell content has to be: (Blue*256+Green)*256+Red  to get the right colour!!!

Gerard

-- 

 

Related Conversations
flashing a white screen while open new tab
cntvertex in Discussions on
13 Replies
Tabs and Dark Mode
cjc2112 in Discussions on
22 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
28 Replies
PacketMon Components are not loading in WAC 1909
HotCakeX in Windows Admin Center on
2 Replies