Exciting code being posted today I know, this is related to a previous post (CityEngine rule creation using Excel) but could come in use in other projects that use hexadecimal colour codes…

First create a new macro called “ConvertToHex” and use the following code:

 

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim str0 As String, str As String
Dim cel As Range
For Each cel In Selection
str0 = Right("000000" & Hex(cel.Interior.Color), 6)
str = Right(str0, 2) & Mid(str0, 3, 2) & Left(str0, 2)
cel = "#" & str & ""
Next cel
done:
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

Customize your toolbar and add this macro as a button, then select a cell and change its fill colour, then making sure you’ve still selected the colour cell press the macro button or run this macro, voila it should write in the cell it’s hexvalue!

2 Comments

  1. Hi – – This code works great.
    After apply the HEX2DEC conversion..
    I am currently setting up a program on an Arduino that has 25RGB LEDs on an arm that rotates 360degress… (Giving my a Circular x/y of 50)

    My challenge is to create a 50×50 image (circular) in A1:AX50
    Setpoint 1 is Y1:Y25 for each of the LEDs respectively – Associating each LED with the corresponding RGB value – As the arm rotates, it will pickup the ‘replacement’ RGB values dependant upon angle and pixel position.

    Cheers for a great step forward.

Comments are closed.