+ Reply to Thread
Results 1 to 7 of 7

vba to change cell color based on date

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-27-2014
    Location
    bristol, england
    MS-Off Ver
    excel 2007
    Posts
    264

    vba to change cell color based on date

    Hi guys what i need is a bit of vba code that can change a range of cells backcolor if an adjacent cell contains a date.

    i.e cells A1 to A55 and B1 to B55, if say B1 contains a date A1 turns GREEN, and if B15 contains a date A15 turns GREEN, but for the code to check all cells in B and change the A adjacent cell in A if the B column cell contains a date, i have tried conditional formatting, but would prefer using vba.

    hope this is clear

    scouse13

  2. #2
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: vba to change cell color based on date

    Hi scouse13 ... Something like this ?

    Sub test()
    
    For Each cell In Range("B1", Range("B" & Rows.Count).End(3))
       If IsDate(cell) Then cell.Offset(, -1).Interior.Color = vbGreen
    Next
    
    End Sub
    If I was able to help, you can thank me by clicking the * Add Reputation under my user name

  3. #3
    Forum Contributor
    Join Date
    06-27-2014
    Location
    bristol, england
    MS-Off Ver
    excel 2007
    Posts
    264

    Re: vba to change cell color based on date

    sorry mate got confused, i am copying data to a sheet, so the dates will be in the copied cells, ie sheet2(sheet were data is copied to) range A1-A50, the code needs to check these cells and change green if they contain a date, sorry for the confusion

  4. #4
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: vba to change cell color based on date

    what about this?

    dim r as range
       for each r in range("b1", range("b1").end(xldown))
          if isdate(trim(r)) then
             r.Offset(0,-1).Interior.Color = rgb(0,255,0)
          end if
       next r
    https://docs.microsoft.com/en-us/off...range.interior

  5. #5
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: vba to change cell color based on date

    Ok, how about now ?
    Sub test()
    
    With Sheets("Sheet2")
       For Each cell In .Range("A1", .Range("A" & Rows.Count).End(3))
          If IsDate(cell) Then cell.Interior.Color = vbGreen
       Next
    End With
    
    End Sub

  6. #6
    Forum Contributor
    Join Date
    06-27-2014
    Location
    bristol, england
    MS-Off Ver
    excel 2007
    Posts
    264

    Re: vba to change cell color based on date

    thanks for that, it works fine and i figured out how to change the font color as well, 1 more question if i may, i need to the code to check different columns, but not in order it needs to check column B and F nd i AND P, IS THIS POSSIBLE, CHANGING FONT COLOR AS WELL

  7. #7
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: vba to change cell color based on date

    Hi scouse13,

    Try the below code ...
    Sub test()
    
    Dim cell As Range, Rg As Range, r$
    
    With Sheets("Sheet2")
       For x = 1 To 3
       r = Choose(x, "B", "F", "P")
       Set Rg = Range(.Cells(1, r), .Cells(Rows.Count, r).End(3))
          For Each cell In Rg
             If IsDate(cell) Then cell.Interior.Color = vbGreen: cell.Font.Color = vbRed
          Next
       Next
    End With
    
    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. How to make a cell adaptively change color based on its date?
    By Thlian in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-11-2018, 05:11 PM
  2. Replies: 5
    Last Post: 12-07-2013, 02:21 PM
  3. Change cell color based on date
    By khaladzki in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-26-2011, 12:02 AM
  4. change cell color based on last edited date
    By pubudu in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-27-2011, 02:11 AM
  5. Cell color change based on date
    By jlaskaris in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-07-2011, 03:36 PM
  6. cell color change based on due date
    By MINAL ZUNKE in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 06-30-2005, 05:05 PM
  7. [SOLVED] Change cell color based on date
    By RRP333 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-02-2005, 02:06 AM

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