+ Reply to Thread
Results 1 to 8 of 8

Setting Color of Range based on adjacent cell color

  1. #1
    Registered User
    Join Date
    11-01-2009
    Location
    Georgia
    MS-Off Ver
    Excel 2003
    Posts
    4

    Thumbs up Setting Color of Range based on adjacent cell color

    This is probably elementary, but I'm struggling and would appreciate any help as I have very little excel VBA experience to draw from.

    I have assembled code which changes the cell color based on a value change in Column A. Column A will contain many different groups of repeating values. This code works well and and I have been able to figure out how to limit the number of colors to only 2. The end result is each set of similar values in column A is visually grouped by one of two alternating colors.

    The number rows in the data set is variable as the data set is extracted from SAP. The number of columns is fixed.

    What I want to do now is set the cell color in columns B through F the same color that was assigned to the row in column A. So if cell A3 is set to colorindex = 6, then I want to set the range of cells B3 to E3 to the same color.

    Here is the code I am using to set the color of the cells in Column A:

    Please Login or Register  to view this content.
    If I can figure this out, I'll next try taking a similar approach with applying borders on the value change.

    Many thanks in advance.
    Attached Files Attached Files
    Last edited by JimE; 11-02-2009 at 10:06 PM. Reason: Add tags, add file, status to Solved

  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: Setting Color of Range based on adjacent cell color

    Welcome to the forum.

    Please take a few minutes to read the forum rules, and then edit your post to add code tags.

    Thanks.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    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: Setting Color of Range based on adjacent cell color

    Hello JimE,

    Welcome to the Forum!

    In VBA it is seldom necessary to have to select an object before an action is undertaken. I have rewritten your macro to access the cells directly.
    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!)

  4. #4
    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: Setting Color of Range based on adjacent cell color

    Similarly,
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    11-01-2009
    Location
    Georgia
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Setting Color of Range based on adjacent cell color

    Hello Leigh, Thank you for taking the time to look at this. I have been unsuccessful in running this code as I keep getting a Run Time 1004 error; application or object defined error. I think it is hanging up on this line:

    For Each Cell In Rng.Rows(1).Offset(-1, 0)

    The code Shg provided runs very well, but I would also like to give this one a shot if the fix is simple.

    I should note, there will be additional columns added to the worksheet which should not get colored when the macro runs. I'd like to just color the rows in columns A through E, regardless if there is data in columns F through J.

    Regards,
    Jim[/SIZE]
    Last edited by shg; 11-01-2009 at 11:35 PM. Reason: deleted spurious quote

  6. #6
    Registered User
    Join Date
    11-01-2009
    Location
    Georgia
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Setting Color of Range based on adjacent cell color

    Quote Originally Posted by shg View Post
    Similarly,
    This works very nicely. You make something so foreign seem so simple.

    Is there a way to make it so the last column is fixed, say column E, instead of using the used range? There will be additional columns added to the data for calculations which should not get colored. I'd like to just color the rows in columns A through E, regardless if there is data in columns F through J.

    Many thanks,
    Jim[/SIZE]
    Last edited by shg; 11-01-2009 at 11:36 PM. Reason: deleted spurious quote

  7. #7
    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: Setting Color of Range based on adjacent cell color

    Is there a way to make it so the last column is fixed, say column E, ...
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    11-01-2009
    Location
    Georgia
    MS-Off Ver
    Excel 2003
    Posts
    4

    Thumbs up Re: Setting Color of Range based on adjacent cell color

    Perfect. Thank you very much.....

+ 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