+ Reply to Thread
Results 1 to 6 of 6

VBA Code to Change Cell Color Based On the Cell Date/Value (like conditional formatting)

  1. #1
    Registered User
    Join Date
    07-18-2010
    Location
    Hong Kong
    MS-Off Ver
    Excel 2003
    Posts
    31

    VBA Code to Change Cell Color Based On the Cell Date/Value (like conditional formatting)

    hi buddy,

    i want to have a code that can change the cell color automatically based on the cell date as follows:

    cell range A1:D1, A10:D10, A20:D20

    condition 1: if the cell value in the ranges above which is early than 1 Oct 13, then the cell color change to blue
    condition 2: if the cell value in the ranges above which is between 1 Oct 13 and 31 Oct 13 both dates included, then the cell color change to red
    condition 3: if the cell value in the ranges above which is between 1 Nov 13 and 30 Nov 13 both dates included, then the cell color change to yellow
    condition 4: if the cell value in the ranges above which is between 1 Dec 13 and 12 Dec 13 both dates included, then the cell color change to orange
    condition 5: if the cell value in the ranges above which is equal to "leakage", then the cell color change to back

    many thanks!
    roy

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

    Re: VBA Code to Change Cell Color Based On the Cell Date/Value (like conditional formattin

    Check out conditional formatting.

    Attach a sample workbook. Make sure there is just enough data to make it clear what is needed. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are demonstrated, mock them up manually if needed. Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

  3. #3
    Registered User
    Join Date
    07-18-2010
    Location
    Hong Kong
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: VBA Code to Change Cell Color Based On the Cell Date/Value (like conditional formattin

    Quote Originally Posted by davesexcel View Post
    Check out conditional formatting.

    Attach a sample workbook. Make sure there is just enough data to make it clear what is needed. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are demonstrated, mock them up manually if needed. Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

    thanks buddy,

    but my office version is 2003, so need vba code for such function, would you help?

    many thanks,
    roy

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

    Re: VBA Code to Change Cell Color Based On the Cell Date/Value (like conditional formattin

    Quote Originally Posted by roy__lam View Post
    thanks buddy,

    but my office version is 2003, so need vba code for such function, would you help?

    many thanks,
    roy
    Where's the sample?

    xl'03 has conditional formatting.....

  5. #5
    Registered User
    Join Date
    07-18-2010
    Location
    Hong Kong
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: VBA Code to Change Cell Color Based On the Cell Date/Value (like conditional formattin

    yes, xl'03 has conditional formatting, but it can only 3 conditions, so that why i want to have to vba code.

  6. #6
    Registered User
    Join Date
    07-18-2010
    Location
    Hong Kong
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: VBA Code to Change Cell Color Based On the Cell Date/Value (like conditional formattin

    i write a code below but don't why can't work perfectly:

    Private Sub cellcolordate()

    Dim cell As Range

    For Each cell In Range("b9:ah10, b15:ak40, b52:ah53, b58:ak83")

    If cell.Value >= 1 And cell.Value < "1/10/2013" Then _
    cell.Interior.Color = vbBlue


    If cell.Value >= "1/10/2013" And cell.value < "31/10/2013" Then _
    cell.Interior.Color = vbRed

    If cell.Value >= "1/11/2013" And cell.Value < "30/11/2013" Then _
    cell.Interior.Color = vbYellow

    If cell.Value >= "1/12/2013" And cell.Value < "31/12/2013" Then _
    cell.Interior.COlor = vbOrange

    If cell.Value = "leakage" Then _
    cell.Interior.Color = vbBlack

    End If

    Next cell
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 3
    Last Post: 11-08-2013, 03:10 PM
  2. Replies: 1
    Last Post: 10-09-2013, 05:05 PM
  3. Replies: 10
    Last Post: 06-19-2013, 05:28 PM
  4. [SOLVED] 2010 Conditional Formatting: Cell Color Change, Based on Ratio (1:1)
    By James_D in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-29-2013, 09:02 PM
  5. Replies: 5
    Last Post: 05-10-2012, 10:49 PM

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