+ Reply to Thread
Results 1 to 14 of 14

Return a cell value as well as its background color

  1. #1
    Registered User
    Join Date
    12-01-2010
    Location
    Pittsburgh, PA
    MS-Off Ver
    Excel 2007
    Posts
    11

    Return a cell value as well as its background color

    Hello all,

    I have a table with the columns: id, company and year, eg:
    2 Dell 1996
    2 Microsoft 1999
    5 Apple 1997
    5 Oracle 1999
    5 Amazon 2000

    I want to look in this table for a certain id and a certain year (some ids show up multiple times with multiple companies and years, I want hte first instance of an id and year match), and then print the company in the appropriate cell in another 2d table where the left column is just IDs and the row at the top is years.

    1995 1996 1997 1998 1999 2000
    2 Dell Microsoft
    5 Apple Oracle Amazon
    I have done this successfully using the following function, but now I also want it to return the color of the cell, not just its value.
    ---
    Please Login or Register  to view this content.
    --------

    I have tried calling a sub from the function to do just that but I don't know how to do it right. I have tried all sorts of other things to no avail. Any help would be appreciated.
    Last edited by ysouljah; 12-06-2010 at 09:29 PM. Reason: added code tags

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Return a cell value as well as its background color

    You cannot change a cell's formatting with a custom function.

  3. #3
    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: Return a cell value as well as its background color

    This is just a guess at what you are trying to do.

    A sample workbook would not go amiss
    It should clearly illustrate your problem and not contain any sensitive data.

    Select a company name in column B to see the result
    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.

  4. #4
    Registered User
    Join Date
    12-01-2010
    Location
    Pittsburgh, PA
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Return a cell value as well as its background color

    Thanks for your response!

    Yes, this is almost what I am trying to do, except when I convert from the first table to the second I want the color to permanently be transferred / highlighted along with it. Right now, in your file, when I select a company name in column 2, the background color appears in the second table briefly/till I select something else.

    I have attached a test file to show how my data was, where I am, and my goal. Since my code already works to place each company in the right cell, any find and match and copy format help to compare Table2 with List1 would work. I have over 2000 distinct IDs so I cannot do this manually.

    Thank you again!
    Attached Files Attached Files
    Last edited by ysouljah; 12-02-2010 at 02:20 PM. Reason: Added test file

  5. #5
    Registered User
    Join Date
    12-01-2010
    Location
    Pittsburgh, PA
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Return a cell value as well as its background color

    Bump No resolution.

  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: Return a cell value as well as its background color

    Hello ysouljah,

    If you are coloring the cells using Conditional Formatting then VBA will not be able to return the cell color. Conditional Formatting does not affect the cell's Interior property.
    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
    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: Return a cell value as well as its background color

    This might be a possible solution.

    Select from Column B

    Somehow I get the feeling that there is a little more to this than you have so far declared.
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    12-01-2010
    Location
    Pittsburgh, PA
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Return a cell value as well as its background color

    This is a work of pure genius!
    I'm going to use your code on the main dataset (I'll be sure to update rows & columns) and will update on whether it still works, so that I can mark this solved, or breaks.
    Thank you so much!

  9. #9
    Registered User
    Join Date
    12-01-2010
    Location
    Pittsburgh, PA
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Return a cell value as well as its background color

    Hi Leith,

    The cells had been colored manually. I think Marcol's solution might work, so I'm going to try it now! Thanks!

  10. #10
    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: Return a cell value as well as its background color

    Hello ysouljah,

    Thanks for update. I mentioned the Conditional Formatting piece because I have written code to change cell colors for people and they said it didn't work. The reason turned out to be they were using Conditional Formatting. It is one of those programming "gems" you find once in a while.

  11. #11
    Registered User
    Join Date
    12-01-2010
    Location
    Pittsburgh, PA
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Return a cell value as well as its background color

    Hi Marcol,

    Thank you again, very much! This worked when I pasted the data into the original worksheet. I'm now trying to figure out to get it work on all worksheets in the file - the data is all arranged the same way. I put in three lines that I thought would make it work for every worksheet, but I was wrong, because it's not working.

    Please Login or Register  to view this content.
    Thank you :-)

  12. #12
    Registered User
    Join Date
    12-01-2010
    Location
    Pittsburgh, PA
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Return a cell value as well as its background color

    Ok so I think I solved it! I noticed that the code was only on Sheet1 when viewing VB developer, so I put the same code on the other sheets too. I took out the worksheet lines I added.

    I didn't change the variable names or anything though. Do you think I should, if that could cause unexpected problems later?

    Thank you very much Should be able to mark this as solved very soon!

  13. #13
    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: Return a cell value as well as its background color

    Try the code in the Workbook module then in the Sheet Selection Change event
    Option Explicit
    Please Login or Register  to view this content.

    This should do as you need in all the sheets in your workbook, provided they all have exactly the same layout.

    I have added this line
    Please Login or Register  to view this content.
    This means the code will fire in all sheets except "Sheet3", take this line out if it is not required.
    I put it in only as an example to illustrate that you can use that feature to do different things with different groups of sheets. For instance you could use a Select Case sh.Name statement to apply code to different groups of sheets.

    If each sheet is different and needs individually coded then use the Worksheet Modules for each sheet.
    If groups or all sheets use the same code then use the Workbook Modules.

    There is no need to change the variable names.

    Hope this helps
    Attached Files Attached Files
    Last edited by Marcol; 12-06-2010 at 05:00 AM. Reason: Workbook example added

  14. #14
    Registered User
    Join Date
    12-01-2010
    Location
    Pittsburgh, PA
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Return a cell value as well as its background color

    Thank you :-) I have learned so much about working with subs these past few days, and I appreciate all your help!

    I'm going to mark this as "SOLVED" now. Thanks!

+ 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