Excel

Create a list of RGB and HEX codes

Ease of Use

Easy

Version tested with

2002,2000 

Submitted by:

wnazzaro

Description:

When you run this code, two rows of the open worksheet will be filled in with RGB codes and the corresponding HEX codes in the row below. 

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 every translation for you from 0 to 255. 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 will have every translation at your fingertips. 

Code:

instructions for use

			

Option Explicit Public Sub RGB2Hex() 'Always include error handling. On Error GoTo MyHandler: 'The rgb numbers represented by i Dim i As Integer 'This will hold the Hex code. Dim Output As String 'First we ask if you want to continue, since the code will take two rows 'of your sheet, and overwrite anything currently there. Dim YN As Byte YN = MsgBox("This will take up two full rows on your worksheet." & vbCrLf & _ "Do you want to continue?", vbYesNo, "RGB & HEX") 'If you don't say Yes, the code will exit. (Yes = 6) If Not YN = 6 Then Exit Sub 'This error handling is to make sure you enter a number in the InputBox below. On Error GoTo NotANumber: 'The program asks which row you would like the codes to be placed. Dim whereStart As Long whereStart = InputBox("In which row would you like the series to start?", "RGB & HEX") 'The number must be between 1 & 65,535 'since there are a finite number of rows on a sheet If whereStart < 1 Or whereStart > 65535 Then GoTo NotANumber: 'If we've made it this far without a number error, we can go back 'to using the simple error handler. On Error GoTo MyHandler: 'Since HEX codes for colors are two digits, this returns some numbers with 'leading zeroes. Excel thinks that's a problem, but it isn't. 'This will turn off the Error Checking for a number stored as text. Application.ErrorCheckingOptions.NumberAsText = False 'Loop through the RGB numbers 0 to 255. For i = 0 To 255 'The first row is the RGB number. With Cells(whereStart, i + 1) .Value = i 'It looks nice centered. .HorizontalAlignment = xlCenter End With 'Use the native function to find the HEX code. Output = Hex(i) 'Now find those numbers that need the leading zero. If Len(Output) = 1 Then Output = "0" & Output With Cells(whereStart + 1, i + 1) 'Format the second row as text to return a leading zero when needed. .NumberFormat = "@" 'Set the cell to Output. .Value = Output 'I still think it looks better centered. .HorizontalAlignment = xlCenter End With Next Exit Sub 'Handling the number issues. NotANumber: Err.Clear MsgBox "You must enter a number between 1 and 65,535", vbOKOnly, "Enter a number" Exit Sub 'Handling anything else that might crop up. MyHandler: MsgBox Err.Description Err.Clear End Sub

How to use:

  1. Open Excel
  2. Go to Tools - Macro - Visual Basic Editor
  3. Under VBAProject(Book 1), right click then go to Insert and click on Module
  4. Paste the code above into the window that opens
  5. Go to File - Close and Return to Microsoft Excel
 

Test the code:

  1. Go to Tools - Macro - Macros
  2. Click on RGB2Hex and hit run
  3. You must say Yes to the message box that appears for the code to run. Make sure you have two free adjacent rows, since the code will overwrite any data you have.
  4. Tell the code which row you want to use. This will also use the row right below the one you choose.
  5. Click OK.
  6. The code will run and enter numbers on two rows, the row you named is the RGB code, the row below is the corresponding HEX code.
 

Sample File:

Hex Codes.zip 21.57KB 

Approved by mdmackillop


This entry has been viewed 151 times.

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