+ Reply to Thread
Results 1 to 34 of 34

Color formatting based on date in another cell with VB

  1. #1
    Registered User
    Join Date
    03-23-2012
    Location
    michigan
    MS-Off Ver
    Excel 2003
    Posts
    20

    Color formatting based on date in another cell with VB

    I have been trying to get a column of cells that have a date assigned to then to change colors based on a date in another cell.
    I cannot use conditional formatting because the cells are populated with a macro that I cannot change and has the color formats included in the macro.
    I want the macro to run whenever the worksheet is opened. this would then overwrite the color formats that the other macro put in.

    The "master date" is in C1, the column of cells is in the range C8:C116.
    I need 2 colors. Yellow if the date in C1 is 1 to 3 days later than the dates in the column. Red if the date in C1 is the same or earlier than the dates in the column.
    I'm a newbie at VB and any help would be greatly appreaciated.

    Thank You

  2. #2
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Lightbulb Re: Color formatting based on date in another cell with VB

    Hello
    The following code should work placed in the ThisWorkbook module, Workbook Open event. Assuming you're referencing Sheet1.

    Please Login or Register  to view this content.
    Hope this helps
    DBY

  3. #3
    Registered User
    Join Date
    03-23-2012
    Location
    michigan
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Color formatting based on date in another cell with VB

    Thank You,

    I an getting the error "subscript out of range"
    I changed the With Worksheets("Sheet1") to With Worksheets("Sheet17")that is my sheet number

    I have alot going on in the Thisworkbook, would it be possible to make it work on the opening of the workshet?
    that codepage is empty

    Thank You

  4. #4
    Registered User
    Join Date
    03-23-2012
    Location
    michigan
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Color formatting based on date in another cell with VB

    If it makes a difference I am using Excel 2002 sp3

    Thanks Again!

  5. #5
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: Color formatting based on date in another cell with VB

    Not sure why it's not working on the Workbook open event but yes you can try the code on the Worksheet Activate event on Sheet17. The version shouldn't make any difference but I'll check.

    DBY

  6. #6
    Registered User
    Join Date
    03-23-2012
    Location
    michigan
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Color formatting based on date in another cell with VB

    Also Ijust thought of something, it really needs to be on the worksheet open, this worksheet gets changed after the workbook is opened and the colors to be changed whenever the worksheet gets opened

    Thank you

  7. #7
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: Color formatting based on date in another cell with VB

    I don't have any problems in either 2003 or 2007. Seems to work fine.

  8. #8
    Registered User
    Join Date
    03-23-2012
    Location
    michigan
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Color formatting based on date in another cell with VB

    Quote Originally Posted by DBY View Post
    Not sure why it's not working on the Workbook open event but yes you can try the code on the Worksheet Activate event on Sheet17. The version shouldn't make any difference but I'll check.

    DBY
    I just moved it to the worksheet and I do not get any errors, however it does not do anything either

    thank you

  9. #9
    Registered User
    Join Date
    03-23-2012
    Location
    michigan
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Color formatting based on date in another cell with VB

    Could it be because the column has conditional formating set to it from the other macro that I cannot change that I mentioned in the post?

  10. #10
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: Color formatting based on date in another cell with VB

    If there is conditional formatting already being applied to the cells then it might well affect the macro. Difficult to say without seeing the workbook what exactly is happening.

  11. #11
    Registered User
    Join Date
    03-23-2012
    Location
    michigan
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Color formatting based on date in another cell with VB

    Would it be possible for the macro you sent to delete all conditional formatting, then apply he colors?
    I may be able to send you the worksheet, but my boss will not let me send the whole workbook?

    Thank You

  12. #12
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Lightbulb Re: Color formatting based on date in another cell with VB

    Try the amended code:

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    03-23-2012
    Location
    michigan
    MS-Off Ver
    Excel 2003
    Posts
    20

    Red face Re: Color formatting based on date in another cell with VB

    I changed it to this and it mostly works THANK YOU
    The problems I have is I have to activate the sheet 2 times before it takes affect.
    Any blank cells are red.
    Last problem is is there a way so that if the dates does not meet the parameters for red or yellow that they would go back to white?

    Thank You very much for doing this so fast!


    Please Login or Register  to view this content.

  14. #14
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: Color formatting based on date in another cell with VB

    Not sure why the sheet has to be activated twice but a least you've got something working. Try this amended code:

    Please Login or Register  to view this content.
    If the cells are blank they should now remain clear of a fill colour. I've also added the Screen updating line to make it a bit more efficient.

    DBY

  15. #15
    Registered User
    Join Date
    03-23-2012
    Location
    michigan
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Color formatting based on date in another cell with VB

    This doesn't change the colors at all

  16. #16
    Registered User
    Join Date
    03-23-2012
    Location
    michigan
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Color formatting based on date in another cell with VB

    I changed to this and now the blanks are white.
    What is weird is that once the color has been changed to yellow or red it will not change back to white

    Thanks for all your help

    Please Login or Register  to view this content.

  17. #17
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: Color formatting based on date in another cell with VB

    I'm at a loss to know what to suggest. It works perfectly on my PC. I've attached my example file for you to look at. See if this one is working on your PC. Remember if the Workbook is on Sheet 17 when it opens the macro won't run. It only runs when Sheet 17 is selected from another tab.
    Attached Files Attached Files

  18. #18
    Registered User
    Join Date
    03-23-2012
    Location
    michigan
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Color formatting based on date in another cell with VB

    Maybe its my excel 2002, your example if I change the date after a color has been applied does not go back to white either.
    I'm totally confused??

  19. #19
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: Color formatting based on date in another cell with VB

    Sorry Double Post. Didn't think the first one had loaded.
    Last edited by DBY; 04-27-2012 at 04:45 PM.

  20. #20
    Registered User
    Join Date
    03-23-2012
    Location
    michigan
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Color formatting based on date in another cell with VB

    I just realized that if the date starts out white it will change to red or yellow, when changing a red date to a yellow date the color will turn to yellow. when trying to change this yellow date to a white date it will not change. This happens on your sheet also.

    Strange

  21. #21
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: Color formatting based on date in another cell with VB

    Macro doesn't work like conditional formatting on the sheet. You have to run it again by deactivating the sheet and reactivating.

  22. #22
    Registered User
    Join Date
    03-23-2012
    Location
    michigan
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Color formatting based on date in another cell with VB

    I understand that. It does not work when changing sheets and reactivating. Eveything works except the color will not change back to white

  23. #23
    Registered User
    Join Date
    03-23-2012
    Location
    michigan
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Color formatting based on date in another cell with VB

    I changed to this and now I THINK it is all working

    Thank you

    Please Login or Register  to view this content.

  24. #24
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: Color formatting based on date in another cell with VB

    I think I'm getting what you're saying. Try this:

    Please Login or Register  to view this content.

  25. #25
    Registered User
    Join Date
    03-23-2012
    Location
    michigan
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Color formatting based on date in another cell with VB

    Works Great!

    Thank You for all your work

  26. #26
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: Color formatting based on date in another cell with VB

    Glad we got there in the end.

    Regards DBY

  27. #27
    Registered User
    Join Date
    03-23-2012
    Location
    michigan
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Color formatting based on date in another cell with VB

    Hello, what you did is working good. Just wondering, would it be hard to make the columns B,D and E turn the same color as C?

    Thank you

  28. #28
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: Color formatting based on date in another cell with VB

    Hi
    Glad it's working. I think if you amend the code to include the columns of B, D and E it should work.

    Please Login or Register  to view this content.
    DBY

  29. #29
    Registered User
    Join Date
    03-23-2012
    Location
    michigan
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Color formatting based on date in another cell with VB

    All this does is turn any used cell in column D red.
    This is what I thought might work also but of coarse can't be that easy

    Thank You

  30. #30
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: Color formatting based on date in another cell with VB

    Hello
    I've attached another test file. This one runs the macro from buttons rather than the sheet activate event, just for test purposes. There's also a clear button to reset the colours. It all seems to work. So again I'm not sure what's going on with your Workbook. Try the test sheet out and see if the code works for you. If it does then there's something on your Workbook that's preventing it from running correctly. Is it possible to upload a sample without anything sensitive or confidential contained within?

    DBY
    Attached Files Attached Files
    Last edited by DBY; 05-03-2012 at 11:06 AM. Reason: Changed attachment for 2003 version

  31. #31
    Registered User
    Join Date
    03-23-2012
    Location
    michigan
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Color formatting based on date in another cell with VB

    I think the problem is that my columns have different formats. My master date column is set to date and some of the other columns are text or numbers.
    When I change the master date to a number format then some columns work.
    Here is a stripped down sheet.

    Thank You
    Attached Files Attached Files

  32. #32
    Registered User
    Join Date
    03-23-2012
    Location
    michigan
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Color formatting based on date in another cell with VB

    Hello,
    After looking at it more I think I need a way to say if a cell in column C is yellow or Red then cells in the same row in columns B,D,E and F are also yellow or red.
    Thanks for all your help

  33. #33
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: Color formatting based on date in another cell with VB

    Hi
    Sorry for the late reply. Having seen your example it all becomes clearer. I didn't realize you were referencing anything other than dates in the range. I've amended the code to turn the rows across columns B:F coloured depending on the value in column C compared to cell C1.

    Please Login or Register  to view this content.
    Hope this does the trick
    DBY
    Last edited by DBY; 05-04-2012 at 03:42 PM.

  34. #34
    Registered User
    Join Date
    03-23-2012
    Location
    michigan
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Color formatting based on date in another cell with VB

    Works Great,
    Thank You for all your help

    Bill
    Last edited by cadman56; 05-08-2012 at 01:58 PM.

+ 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