Set a Cell background colour with hex value from another cell

Excelerate3305

New Member
Joined
Oct 7, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
This is my very first post so please be gentle. I was wondering if it is possible to set the background colour of a cell with a hex value that I would enter from an adjacent cell?? I suspect some form of VBA code will be required?
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
I have no idea how you want to trigger the VBA but this is manually triggered

Select a cell and next to the right of selected cell write a 6 digit hex value from 000000 to FFFFFF (representing RGB value for each to digit accordingly)
Alt+F8 and run the macro
VBA Code:
Sub ChgColor()

Dim strHex As String

strHex = Selection.Offset(0, 1)
Selection.Interior.Color = HexToRGB(strHex)

End Sub

Function HexToRGB(sHexVal As String) As Long
    
Dim lRed As Long
Dim lGreen As Long
Dim lBlue As Long

lRed = CLng("&H" & Left$(sHexVal, 2))
lGreen = CLng("&H" & Mid$(sHexVal, 3, 2))
lBlue = CLng("&H" & Right$(sHexVal, 2))

HexToRGB = RGB(lRed, lGreen, lBlue)

End Function
 
Upvote 0
Thanks for the VBA code.

I have attached a copy of the spreadsheet I will be using. I would like to enter the Hex# in cell J8 then have the corresponding colour appear in cell K8. I would like to have this repeated for every row that is populated. I will insert a Form Control button in the spreadsheet then assign the VBA macro to it. Will the original VBA code still work??

Thomas Paint.xlsx
ABCDEFGHIJKL
1Paint Inventory List
2
3Name:
4Address:
5Total Value of Inventory:
6
7Paint Name:Item DescriptionManufacturerSerial / ID numberDate PurchasedWhere PurchasedPurchase PriceQuantity in StockInventory ValueHex #Colour SampleNotes
8
9
10
11
Sheet1
 
Upvote 0

Attach a worksheet sample with some filled rows at least in particular with the hex color …​
 
Upvote 0
Thanks for the VBA code.

I have attached a copy of the spreadsheet I will be using. I would like to enter the Hex# in cell J8 then have the corresponding colour appear in cell K8. I would like to have this repeated for every row that is populated. I will insert a Form Control button in the spreadsheet then assign the VBA macro to it. Will the original VBA code still work??

Thomas Paint.xlsx
ABCDEFGHIJKL
1Paint Inventory List
2
3Name:
4Address:
5Total Value of Inventory:
6
7Paint Name:Item DescriptionManufacturerSerial / ID numberDate PurchasedWhere PurchasedPurchase PriceQuantity in StockInventory ValueHex #Colour SampleNotes
8
9
10
11
Sheet1

Here is better way to implement what you want
Put the macro below under Sheet1 module. As you can see the working range is set to Range("J8:J16"). You can change the operating range. Once Enter is pressed, the color to the next cell is produce. No need to have any form to activate.



VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim strHex As String

If Not Intersect(Range("J8:J16"), Target) Is Nothing Then
    If Target.Value = "" Then
        Target.Offset(0, 1).Interior.Color = xlNone
        Exit Sub
    End If
    strHex = Target.Value
    Target.Offset(0, 1).Interior.Color = HexToRGB(strHex)
Else
    Exit Sub
End If

End Sub

Function HexToRGB(sHexVal As String) As Long
   
Dim lRed As Long
Dim lGreen As Long
Dim lBlue As Long

lRed = CLng("&H" & Left$(sHexVal, 2))
lGreen = CLng("&H" & Mid$(sHexVal, 3, 2))
lBlue = CLng("&H" & Right$(sHexVal, 2))

HexToRGB = RGB(lRed, lGreen, lBlue)

End Function
 

Attachments

  • Worksheet.jpg
    Worksheet.jpg
    143.9 KB · Views: 369
Upvote 0
Here is better way to implement what you want
Put the macro below under Sheet1 module. As you can see the working range is set to Range("J8:J16"). You can change the operating range. Once Enter is pressed, the color to the next cell is produce. No need to have any form to activate.



VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim strHex As String

If Not Intersect(Range("J8:J16"), Target) Is Nothing Then
    If Target.Value = "" Then
        Target.Offset(0, 1).Interior.Color = xlNone
        Exit Sub
    End If
    strHex = Target.Value
    Target.Offset(0, 1).Interior.Color = HexToRGB(strHex)
Else
    Exit Sub
End If

End Sub

Function HexToRGB(sHexVal As String) As Long
  
Dim lRed As Long
Dim lGreen As Long
Dim lBlue As Long

lRed = CLng("&H" & Left$(sHexVal, 2))
lGreen = CLng("&H" & Mid$(sHexVal, 3, 2))
lBlue = CLng("&H" & Right$(sHexVal, 2))

HexToRGB = RGB(lRed, lGreen, lBlue)

End Function
Thanks so much. At present I am not sure how many rows I will require. Can the VBA code be changed in order to automatically increase the range as additional rows are inserted??
 
Upvote 0

Yes but it seems you have missed post #4 …​
 
Upvote 0
Thanks so much. At present I am not sure how many rows I will require. Can the VBA code be changed in order to automatically increase the range as additional rows are inserted??
I have no idea if you have something else underneath J16. Otherwise you can put J10000 or all the way down to J1048576 ;)
 
Upvote 0

Forum statistics

Threads
1,215,467
Messages
6,124,984
Members
449,201
Latest member
Lunzwe73

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