+ Reply to Thread
Results 1 to 18 of 18

Extract Cell Font/Background Colour

  1. #1
    Registered User
    Join Date
    06-06-2010
    Location
    vancouver
    MS-Off Ver
    Excel 2003
    Posts
    9

    Extract Cell Font/Background Colour

    I have several cells, each with a different font and background colour. I am wondering if it is possible for me to extract the RGB value of the font/background colour of each cell and how I would do this?
    Last edited by icu222much; 07-02-2010 at 07:15 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Extract Cell Font/Background Colour

    Welcome to the forum
    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: Extract Cell Font/Background Colour

    This bit of code will show the colorindex for the A1
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    06-06-2010
    Location
    vancouver
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Extract Cell Font/Background Colour

    Thank you for the speedy reply guys.



    Quote Originally Posted by shg View Post
    Welcome to the forum
    Please Login or Register  to view this content.
    I tried the code but it just gave me 2 wierd numbers. I have set my cell A1 with font colour Red, and background colour Yellow. It reported that my font colour is 66047and my background colour is 65535. This does not seem to be RGB values unless there is some wierd converstion thing that is happening that I am unaware of.



    Quote Originally Posted by davesexcel View Post
    This bit of code will show the colorindex for the A1
    Please Login or Register  to view this content.
    From my understanding, ColorIndex only allows me to play with pre-determind Excel values. Am I correct? Almost all of my colours will be outside of this range I believe.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Extract Cell Font/Background Colour

    RGB values are stored as eight bits each in a long: 00BBGGRR. See if this speaks to you:
    Please Login or Register  to view this content.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Extract Cell Font/Background Colour

    Hello icu222much,

    I wrote this macro to provide the RGB value for any Excel or System color value. It reurs the values in a single dimension array where subscript (0) = Red, (1) = Green, (2) = Blue. Copy this into a separate VBA module in your VB Project.
    Please Login or Register  to view this content.

    Code Example
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  7. #7
    Registered User
    Join Date
    06-06-2010
    Location
    vancouver
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Extract Cell Font/Background Colour

    @ shg:

    Your code seems to sort of work. It returns an incorrect hex value. My font colour is Red, but it kept on returning the hex representation of Teal. I further investigated the value and noticed that instead of getting RGB, I am getting BRG. That is still fine, as I can just move the values around to get RGB. Thank you sgh.


    @ Leith Ross

    Thank you for your code. When I went to run your code, I got a "Type Mis-match" error in the RGBTest() on the line "R = Color(0).
    Last edited by shg; 07-01-2010 at 12:27 AM. Reason: deleted spurious quotes

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Extract Cell Font/Background Colour

    It returns an incorrect hex value.
    I don't think so.
    My font colour is Red, but it kept on returning the hex representation of Teal.
    Only if the cell was formatted as teal and the red was the result of conditional fomatting.
    instead of getting RGB, I am getting BRG
    I don't think so.

  9. #9
    Registered User
    Join Date
    06-06-2010
    Location
    vancouver
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Extract Cell Font/Background Colour

    Quote Originally Posted by shg View Post
    I don't think so.

    Only if the cell was formatted as teal and the red was the result of conditional fomatting.

    I don't think so.
    Could it be because of how the settings on my computer is set? I verified the hex value twice from two separate sources and they reported that the hex value was teal.

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Extract Cell Font/Background Colour

    Post a workbook that shows this.

  11. #11
    Registered User
    Join Date
    06-06-2010
    Location
    vancouver
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Extract Cell Font/Background Colour

    I am home right now, so I have re-created the workbook. I have copied your code, and populated cell A1 with:

    Font: Red
    Background: Yellow

    The hex that is reported is:

    Font:
    Background: Blue
    Background: Teal

    Link to workbook:
    http://www.sfu.ca/~jca41/stuph/testA.xls

  12. #12
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Extract Cell Font/Background Colour

    When I run this:
    Please Login or Register  to view this content.
    ... on that workbook, this prints in the Immediate window:

    Please Login or Register  to view this content.
    ... which is exactly correct.

  13. #13
    Registered User
    Join Date
    06-06-2010
    Location
    vancouver
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Extract Cell Font/Background Colour

    Quote Originally Posted by shg View Post
    When I run this:
    Please Login or Register  to view this content.
    ... on that workbook, this prints in the Immediate window:

    Please Login or Register  to view this content.
    ... which is exactly correct.
    Those are the two hex values I get as well, but they do not represent red/yellow.
    0000FF = Blue
    00FFFF = Teal

  14. #14
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Extract Cell Font/Background Colour

    Hello icu222much,

    What processor does your computer use?

  15. #15
    Registered User
    Join Date
    06-06-2010
    Location
    vancouver
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Extract Cell Font/Background Colour

    Both my work and home computer uses E8400.

  16. #16
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Extract Cell Font/Background Colour

    Hello icu222much,

    Since your are Intel cpu's, all data is stored internally in 'little endian" format. Click this link to read more on this subject. It will help clear up the confusion.

  17. #17
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Extract Cell Font/Background Colour

    A Long is a Long, irrespective of endianness.

    As I said before, RGB values are stored as 00BBGGRR, meaning the red value is in the least significant eight bits, green in the next, blue in the next.

  18. #18
    Registered User
    Join Date
    06-06-2010
    Location
    vancouver
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Extract Cell Font/Background Colour

    Quote Originally Posted by shg View Post
    A Long is a Long, irrespective of endianness.

    As I said before, RGB values are stored as 00BBGGRR, meaning the red value is in the least significant eight bits, green in the next, blue in the next.
    Sounds good. Thank you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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