Colour by code in Excel with VBA - Part 2

Colour by code in Excel with VBA - Part 2

Have you ever wanted to sample colours from a workbook to then use for other applications , like web pages, BI dashboards like QlikSense / OBIEE etc. , or even in Excel / VBA forms themselves?

Look no further, the following quick bit of code should help you achieve that with a very simple UDF that can return the right colour formatting for you with a copy/paste. 

In recent years, Excel has added huge improvements to functionality in terms of sorting / filtering tables by colour,  conditional formatting and palettisation of the tint/shades to be used with corporate themes.

There are some aspects of this that are inaccessible via the standard excel, namely - returning the hex colour value of a cell to use in HTML .  For example, if your reporting is a hybrid of excel workbooks and common BI dashboards (Qliksense/ , Tableau  / OBIEE etc. )  - with the code below, you'll be able to quickly mock up colours for charts in excel to use in your favourite BI tool, or simply just find the true RGB/HEX value of a particular custom colour in a sheet, to use in forms or across branding to keep themes consistent. 

 Here's the code 




Function GetCellColour(Rng As Range, Optional formatType As Integer = 0) As Variant
    Dim colorVal As Variant
    colorVal = Cells(Rng.Row, Rng.Column).Interior.Color
    
    ':: Return error if more than one cell selected.
    If Rng.Cells.Count = 1 Then
    
    
    Select Case formatType
        Case 1  ' ::: WEB HEX :::
            GetCellColour = ZeroPad(Hex((colorVal Mod 256)), 2) & ZeroPad(Hex(((colorVal \ 256) Mod 256)), 2) & ZeroPad(Hex((colorVal \ 65536)), 2)
        Case 2  ' ::: RGB :::
            GetCellColour = (colorVal Mod 256) & ", " & ((colorVal \ 256) Mod 256) & ", " & (colorVal \ 65536)
        Case 3  ' ::: EXCEL COLOUR INDEX :::
            GetCellColour = Cells(Rng.Row, Rng.Column).Interior.ColorIndex
        Case 4  ' ::: VBA USERFORM HEX :::
            GetCellColour = "&H00" & ZeroPad(Hex((colorVal \ 65536)), 2) & ZeroPad(Hex(((colorVal \ 256) Mod 256)), 2) & ZeroPad(Hex((colorVal Mod 256)), 2) & "&"
        
        ' :: COLOUR COMPONENTS ::
        Case 5  ' ::: RED RGB INT 0-255 :::
            GetCellColour = colorVal Mod 256
        Case 6  ' ::: GRN RGB INT 0-255 :::
            GetCellColour = ((colorVal \ 256) Mod 256)
        Case 7  ' ::: BLU RGB INT 0-255 :::
            GetCellColour = (colorVal \ 65536)
        
        Case Else
            GetCellColour = colorVal
    End Select
    
    Else
    ':: Return error if multiple cells picked!
        GetCellColour = "Select one cell!"
    
    End If
    
End Function



Function ZeroPad(Text As String, Cnt As Integer) As String
' :: Text is the string to pad  ::
' :: Cnt is the length to pad to, for example  ZeroPad(12,3) would return a string '012' , Zeropad(12,8) would return '00000012' etc..  ::

Dim StrLen As Integer, StrtString As String, Padded As String, LP As Integer

StrLen = Len(Trim(Text))  '::  CLEAR WHITESPACE IF LEADING SPACES/TABS  ETC.


':: CONDITIONALLY PREFIX ZEROS IF LENGTH OF STRING IS LESS THAN LENGTH OF OUTPUT STRING SPECIFIED
    If StrLen < Cnt Then
    
        For LP = 1 To Cnt - StrLen
        
            Padded = Padded & "0"
        
        Next LP
    
    End If

ZeroPad = Padded & Trim(Text)


End Function



Pretty simple, takes in a range  ( of one cell ) and returns , based on the parameter, the various colour codes to use in either web or other applications.  

The function has an optional parameter, which returns a specific type or the excel generic colour number if left empty. 

ColourNone1234567 
Apples528793600B0500, 176, 8014&H0050B000&017680 
Pears350677254823584, 130, 5312&H00358254&8413053 
Oranges4766194F2B948242, 185, 7244&H0048B9F2&24218572 
Peaches8892401F1AF87241, 175, 13540&H0087AFF1&241175135 
Bananas6740479FFD966255, 217, 10240&H0066D9FF&255217102 
Lemons65535FFFF00255, 255, 06&H0000FFFF&2552550 
Cherries255FF0000255, 0, 03&H000000FF&25500 
Grapes104981607030A0112, 48, 16047&H00A03070&11248160 
          

 

Parameter values breakdown as below :

  1. Traditional HTML 'HEX' value 
  2. Traditional comma separated  Red,Green,Blue integer (0-255) values
  3. Traditional Excel colour index ( for the original palletised colours )
  4. The non-standard HEX format used in Excel / Office userforms for control colours
  5. Individual Red Integer component of 2 ( RGB ) 
  6. Individual Green Integer component of 2 ( RGB ) 
  7. Individual Blue Integer component of 2 ( RGB ) 

This should give you options to produce colour tables / outputs to then transfer into other applications, like QlikSense, OBIEE etc, and keep consistent colours across multiple formats.