Excel

Translate RGB codes to HEX #rrggbb

Ease of Use

Easy

Version tested with

2003 

Submitted by:

wnazzaro

Description:

Add this code to the change event of a worksheet and it will enter the equivalent HEX code of the number entered in the cell to the right of the entered number. If a number outside the range of 0-255 or text is entered, nothing will change on the sheet. 

Discussion:

The company I work for has specified colors to use with any media. While setting up an intranet site, I realized I needed the HEX codes for background colors from the RGB codes the company provided. Searching the internet did not provide me with a thorough understanding of the translation of RGB codes to HEX codes. With this utility, you won't need to find the translation, this will provide it for you. While the Windows calculator will also do this conversion, the calculator can only give one answer at a time. You can enter several numbers into the spreadsheet and find the translation. My co-worker who had done the translation of company colors misread his handwriting and used 70 for 7D, 05 for D5 and F8 instead of FB. With this utility, you won't be forced to do one translation at a time. 

Code:

instructions for use

			

Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) 'Simple error handling, but effective. On Error GoTo MyHandler: 'If the range of Target is more than one cell, this will error 'unless this only runs if there is only one member of the range. If Target.Count = 1 Then 'IsNumeric will check that the entry can be evaluated as a number. 'Also, this code is set for RGB and HEX 'so it will only evaluate a number between 0 and 255. If IsNumeric(Target) And Target <= 255 And Target >= 0 Then 'Always a good idea to turn off events during a change event 'so that the event doesn't run during the execution of this code. Application.EnableEvents = False 'Make the change in the adjecent cell. With Target.Offset(, 1) 'Change the cell format to text so the possible leading zero will display. .NumberFormat = "@" 'Text is aligned left, I like right aligned better. .HorizontalAlignment = xlRight 'This uses the native VBA function Hex() to evaluate the entered number. .Value = Hex(Target) End With 'However, I want the leading zero to display. If Len(Target.Offset(, 1)) = 1 Then Target.Offset(, 1) = "0" & Hex(Target) End If 'Remember to turn events back on. Application.EnableEvents = True End If End If Exit Sub 'Like I said, it's simple but effective error handling. 'Since this code only changes the value of one cell, 'there's not much trouble that an error could cause. MyHandler: Err.Clear 'If the error occurs after events were turned off, you will want to make sure 'Events are turned back on. Application.EnableEvents = True End Sub

How to use:

  1. Open Excel
  2. Go to Tools - Macro - Visual Basic Editor
  3. Click Visual Basic Editor
  4. Under the folder Microsoft Excel Objects, double click Sheet1 (Sheet1)
  5. Paste the code in the sheet that opens
  6. Close the Visual Basic screen
 

Test the code:

  1. Enter a number between 0-255 anywhere on Sheet1 and hit Enter. If you enter 0, 00 is the HEX code for 0 and will appear.
  2. Enter 255 and hit Enter, FF will appear because FF is the HEX code for 255.
  3. Enter 204 and hit Enter and CC will be the result.
 

Sample File:

HEX codes.zip 9.02KB 

Approved by mdmackillop


This entry has been viewed 136 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express