+ Reply to Thread
Results 1 to 12 of 12

Get Hex code of cell

  1. #1
    Registered User
    Join Date
    07-14-2011
    Location
    Oralndo, Fl
    MS-Off Ver
    Excel 2003
    Posts
    6

    Get Hex code of cell

    I have generated a 50 custom color gradient in Excel 2007. Most of these colors are not preset in the color palette. Is there a vba function to get the hex color code for an individual cell filled with a custom color? Thank you in advance for your help.
    Last edited by g.torrez; 07-14-2011 at 10:08 PM.

  2. #2
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Get Hex code of cell

    See if this sample workbook offers you any ideas.
    Attached Files Attached Files
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  3. #3
    Registered User
    Join Date
    07-14-2011
    Location
    Oralndo, Fl
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Get Hex code of cell

    I was able to use that sheet to generate the rgb values separated by commas for the colors. Could somebody help me write a function to convert these numbers to hex. For example, 255, 0, 0 to FF0000.
    Last edited by g.torrez; 07-14-2011 at 11:02 PM.

  4. #4
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: Get Hex code of cell

    Hi G.Torrez,

    The following macro will tell you the Long, Hex and RGB values for colors in A1:A50. It will put the corresponding values in columns B, C and D, respectively.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    07-14-2011
    Location
    Oralndo, Fl
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Get Hex code of cell

    The macro is generating the hex values in reverse i.e. 22DE92 instead of 92DE22. I am also have a problem with the cells in which the value begins with '0'. Instead of 0000FF, it displays FF. I have attached the file if you would like to take a look. Thank you for your assistance.
    Attached Files Attached Files

  6. #6
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: Get Hex code of cell

    Ok, how about this code:
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    07-14-2011
    Location
    Oralndo, Fl
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Get Hex code of cell

    The 0's now appear, but the hex code is still reversed i.e. 49B715 instead of 15B749. Any thoughts?

  8. #8
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: Get Hex code of cell

    Please Login or Register  to view this content.
    Should fix that.

  9. #9
    Registered User
    Join Date
    07-14-2011
    Location
    Oralndo, Fl
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Get Hex code of cell

    Perfect! Thank you.

  10. #10
    Registered User
    Join Date
    07-14-2011
    Location
    Oralndo, Fl
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Get Hex code of cell

    Ok. There is one issue left. With rgb value 153, 224, 31, the corresponding hex code should be 99E020. The macro produces 9.90E+21 thinking this is scientific format. Can this be fixed?

  11. #11
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Get Hex code of cell

    Have a look at this workbook.

    Run the macro "TestColourCellFromHex" to colour fill Column E

    Copy and paste a coloured cell to a cell in Range L2:L6 the formula in Range M2:M6 will return the hex code and the adjacent cell colour will update to suit.

    Type a hex number in M10 down the adjacent cell in Column N will change accordingly.

    If a scientific number is "detected" precede the hex code with an apostrophe to force a string.
    This is not a problem with the function HexCode() it always returns a string.

    Hope this helps.
    Attached Files Attached Files

  12. #12
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Get Hex code of cell

    Sorry I missed your posted workbook, it still might be useful to look at the workbook I last posted (Post #11).

    I have added this function to your workbook
    Please Login or Register  to view this content.
    Use as a UDF
    In E1
    Please Login or Register  to view this content.
    Drag/Fill Down

    Hope this helps.

    [EDIT]
    None of this will work if the colours are generated by conditional formatting, these colours are only a mask, all of the code you have been given will return the underlying cell colours (the actual cell fill colour).

    A handy way to get C/F colours is to copy the range to Word and then back to Excel.
    The code for doing this without using Word is horrific, at best
    Attached Files Attached Files
    Last edited by Marcol; 07-15-2011 at 06:15 AM. Reason: Added note on C/F colours.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1