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 calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Where would the Hex colour come from, if not a range of cells?
OK, excuse me, I think I was not entirely clear, I have already fixed the wording of the question so that it can be understood in a better way. What I am looking for is to create a function that, from a hexadecimal value, can obtain the fill color of a cell.

I think it's too late to edit it!

Basically I would like to find a function that when I enter any hexadecimal value (either 7 or 6 characters #XXXXXX), it will allow me to get the fill color of the cell where I specify the function
 
Upvote 0
You asked for
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.
Are you now saying that is not what you want?
 
Upvote 0
You asked for

Are you now saying that is not what you want?
Basically I would like to find a function that when I enter any hexadecimal value (either 7 or 6 characters #XXXXXX), it will allow me to get the fill color of the cell where I specify the function
 
Upvote 0
How do you want to enter the hex value?
 
Upvote 0
How do you want to enter the hex value?
The variable where the hexadecimal value is stored can contain all 7 characters (#B0BF1A), or it can also contain only 6 (B0BF1A), in either case, you can use the Replace function to determine the number of characters to define[HexColor = Replace(HexColor, "#", "")], however, I would think it would be more practical to have only 6 characters!
 
Upvote 0
That does not ansewr my question. If the Hex number is not coming from col B, where is it coming from?
 
Upvote 0
That does not ansewr my question. If the Hex number is not coming from col B, where is it coming from?
Ok, I think there has been a misunderstanding, to put it simply, following the data that is in the book, the hexadecimal values would come from the B column.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,378
Members
448,955
Latest member
BatCoder

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