Get the color of a cell from its hexadecimal value

quemuenchatocha

Board Regular
Joined
Aug 4, 2021
Messages
50
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Ejemplo_1.xlsm
ABCDEFGH
1ColorHexadecimalColor CodeRGBRGBName Color
2#0048BAAbsolute Zero
3#B0BF1AAcid green
4#7CB9E8Aero
5#C0E8D5Aero blue
6#B284BEAfrican violet
7#72A0C1Air superiority blue
8#EDEAE0Alabaster
9#F0F8FFAlice blue
10#C46210Alloy orange
11#EFDECDAlmond
12#E52B50Amaranth
13#9F2B68Amaranth (M&P)
14#F19CBBAmaranth pink
15#AB274FAmaranth purple
16#D3212DAmaranth red
17#3B7A57Amazon
18#FFBF00Amber
List Of Colors


Dear Best regards, in search of your best advice I am trying to obtain a function that will allow me to get the color of a cell [A2:A975] from the hexadecimal values recorded in another cell [B2:B975]. So far I have only gotten the following basic lines:

VBA Code:
Sub SetHexCol()
Dim i As Integer, LastRow As Integer
LastRow = Range("B" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
    Cells(i, "A").Interior.Color = HexToRgb(Cells(i, "B"))
Next
End Sub

VBA Code:
Function HexToRgb(HexColor As String) As Long
Dim R As String, G As String, B As String
HexColor = Replace(HexColor, "#", "")
R = CLng("&H" & Left(HexColor, 2))
G = CLng("&H" & Mid(HexColor, 3, 2))
B = CLng("&H" & Right(HexColor, 2))
HexToRgb = rgb(R, G, B)
End Function

My idea is that regardless of the cell where the hexadecimal value is located (not just in column B for my case), the function will allow me to fill a cell with the color resulting from the six-character string given to the function.

Thanking you for your valuable help and suggestions to obtain the desired result, I say goodbye
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
In that case I don't understand what you are asking for. The code you supplied applies the relevant colour to col A based on the Hex code in col B
No, what I am really looking for is a function that allows me to fill the color of a cell from its hexadecimal value.

The lines I have so far allow me to do this but not directly through a UDF, but through a sub()

What I'm looking for is something similar to a function like this one (Change a cell's background color dynamically according to a RGB value stored in other cells), but instead of using the RGB values, I will use the hexadecimal values recorded, in the case of my example, in column "B", to obtain the background color of a cell [column "A"].

Basically I am trying to change the background color of a cell [column A] dynamically, from a hexadecimal value stored in another cell [column B], but from the use of a UDF.

Excuse me if I have not been very clear from the beginning, I tried to save text so as not to extend so much when I asked the question.
 
Upvote 0
Ok, how about
VBA Code:
Function ColourCell(HexColor As String)
Dim R As String, G As String, B As String, x
HexColor = Replace(HexColor, "#", "")
HexColor = Right("000000" & HexColor, 6)
R = Left(HexColor, 2)
G = Mid(HexColor, 3, 2)
B = Right(HexColor, 2)
Clr = Application.Hex2Dec(B & G & R)
x = Evaluate("cellcolour(" & Application.ThisCell.Address & "," & Clr & ")")
ColourCell = ""
End Function
Function CellColour(Cl As Range, Clr As Variant)
Cl.Interior.Color = Clr
End Function
 
Upvote 0
Solution
Dear @Fluff , I want to tell you that you are a genius, you got it right, that's really what I was looking for. Excuse me if I wasn't very clear at first, but I didn't want to be so long explaining the procedure which would imply the use of a lot of text in the message.
Thank you very much for your patience and valuable help, and as always be attentive to resolve the doubts of users.
Once again I thank you for your collaboration, a big hug.
 
Upvote 0
Dear @Fluff , I want to tell you that you are a genius, you got it right, that's really what I was looking for. Excuse me if I wasn't very clear at first, but I didn't want to be so long explaining the procedure which would imply the use of a lot of text in the message.
Thank you very much for your patience and valuable help, and as always be attentive to resolve the doubts of users.
Once again I thank you for your collaboration, a big hug.
dear can u share me the file/sheet where you applied this cell coloring VBA Code as i am also trying to apply the code but its not working with me.
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,111
Members
452,302
Latest member
TaMere

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