Results 1 to 17 of 17

Thread: VBA Question on Date's in Excel

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679

    Question VBA Question on Date's in Excel

    Is it possible to write a code that will look at a range of cells that have dates, and if the dates are 60 days old they will be highlighted like that in the conditional formating.

    I am currently looking through some code I have that work with dates, I have not come across something like this...

    If there are any ideas out there for me I would be appreciative..

    Thank You..

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    You could take a look at the DATEDIFF() function, though I've never used this myself.

    This should do the trick though :
    Code:
    If CDate(Excel.Applicaton.Range("A1").value) > (Date() + 60) then ...
    and I've even tested that one - 1st time this year

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    It is the first time for me as well...

    I tried your suggestion, and nothing happens..

    I don't know If I have to refer to the worksheet in the code.

    As it is now this is what my code looks like. I am not getting any error when I run it, but the formating is not working.

    Sub DateCheck ()
    If CDate(Range("B7").Value) = (Date + 26) Then Range("p7").Value = "Yes"

    End Sub

    There has to be something I am forgetting?????

  4. #4
    Lively Member
    Join Date
    Aug 2000
    Location
    Darlington, United Kingdom
    Posts
    121
    Try this one

    Code:
    Sub CheckDate()
        Dim CellDate As Date
        CellDate = CDate(Range("A1").Value)
        If Date - CellDate > 60 Then Range("A1").Font.Bold = True
    End Sub
    Do Not Underestimate The Power Of Simple VBA

    Maybe Its Just Not Possible, But Then Again What Is Impossible

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    That worked for the one cell range. What I need is for that to scan a range of cells such as :

    Dim CellDate As Date
    CellDate = CDate(Range("B7:B9").Value)
    If Date - CellDate > 10 Then Range("B7:B9").Font.Bold = True

    When I run this code I get a Type Mismatch Error!!

    Also when i try to change the format of the cell to highlight in yelllow when the logic statement is true, I get an error message say that it can not change the Color INdex.

    Any guesses as to how to solve these two problems??

    Thanks for the help so far...

  6. #6
    Lively Member
    Join Date
    Aug 2000
    Location
    Darlington, United Kingdom
    Posts
    121
    If you have multiple cells then you would need to create a loop around these cells to check each one individually..

    Lets try this instead

    VB Code:
    1. Sub DateCheck()
    2.     Dim CellDates(), i As Integer, j As Integer
    3.     'Assuming Cell A1 is the start of this range of dates and we do not know
    4.     'the last cell but the cell after the last cell is empty
    5.     Range("A1").Select
    6.     While Activecell.Value <> ""
    7.         i = i + 1
    8.         Activecell.Offset(1,0).Select
    9.     Wend
    10.     'Now redim the CellDates Array so we can populate it with all values
    11.     ReDim CellDates(1 to i)
    12.     'place each cell into the array
    13.     Range("A1").Select
    14.     For j = 1 to i
    15.         CellDates(j) = "A" & j
    16.     Next j
    17.     'Now loop through this array checking the values of the cell and changing the font to bold
    18.     'and the cell background to yellow
    19.     For j = 1 to i
    20.         If Date - Cdate(Range(Celldates(j)).Value) > 60 then
    21.             Range(CellDates(j)).Font.Bold = True
    22.             Range(CellDates(j)).Select
    23.             With Selection.Interior
    24.                 .ColorIndex = 6
    25.                 .Pattern = xlSolid
    26.                 .PatternColorIndex = xlAutomatic
    27.             End With
    28.         End If
    29.     Next j
    30. End Sub

    That should do the trick
    Do Not Underestimate The Power Of Simple VBA

    Maybe Its Just Not Possible, But Then Again What Is Impossible

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    It looks like this is going to work. the only thing is when I tried the code, I received a Type-mismatch error on this line of code:

    If Date - CDate(Range(CellDates(j)).Value) > 60 Then

    My first cell starts on B7, so I inputted that instead of A1.

    An idea why the error??

  8. #8
    Lively Member
    Join Date
    Aug 2000
    Location
    Darlington, United Kingdom
    Posts
    121
    The value in the cell is invalid... put the code in debug mode and step through until you reach the error..

    What is the cell's value??
    Do Not Underestimate The Power Of Simple VBA

    Maybe Its Just Not Possible, But Then Again What Is Impossible

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    The cell value is 'DATE'....

    When I returned to the worksheet after compiling the code, I noticed that cells A1:A5 were higlighted in yellow with a bold font.

    I thought that if I changed the code to read the first cell as B7 that it would loop from there????

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    The cell value that i gave was referring to the Column heading. It was reading from cell B6 which has the value DATE.

  11. #11
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    try a for..next loop with Cells() instead
    A1 is 1,1
    B7 is 7,2
    Cells(row,column)

    Sub CheckDate()
    Dim CellDate As Date
    for x = 7 to 9
    CellDate = CDate(Cells(x,2).Value)
    If Date - CellDate > 60 Then cells(2,x).Font.Bold = True
    End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  12. #12
    Lively Member
    Join Date
    Aug 2000
    Location
    Darlington, United Kingdom
    Posts
    121
    Not unless you change this aswell

    VB Code:
    1. For j = 1 to i
    2.         CellDates(j) = "A" & j
    3.     Next j

    to this
    VB Code:
    1. For j = 1 to i
    2.         CellDates(j) = "B" & j + 6
    3.     Next j

    If you are picking up a value in a cell that does not equal a date value then you will need to error trap like so

    VB Code:
    1. For j = 1 to i
    2.         On Error Goto InvalDate
    3.         If Date - Cdate(Range(Celldates(j)).Value) > 60 then
    4.             Range(CellDates(j)).Font.Bold = True
    5.             Range(CellDates(j)).Select
    6.             With Selection.Interior
    7.                 .ColorIndex = 6
    8.                 .Pattern = xlSolid
    9.                 .PatternColorIndex = xlAutomatic
    10.             End With
    11.         End If
    12.         On Error Goto 0
    13.     Next j
    14.     Exit Sub
    15.  
    16. InvalDate:
    17.     msgbox Err.Description
    18.     Exit Sub
    19. End Sub

    I've just tried my code on a full column of random dates and and it work no problems
    Do Not Underestimate The Power Of Simple VBA

    Maybe Its Just Not Possible, But Then Again What Is Impossible

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    Now I am receiving a Subscript Out of Range error. When I run the debugger it brings up this line of code in yellow:

    ReDim CellDates(1 To i)

  14. #14
    Lively Member
    Join Date
    Aug 2000
    Location
    Darlington, United Kingdom
    Posts
    121
    I'll try a re-create your spreadsheet and see what I can change in the code for you
    Do Not Underestimate The Power Of Simple VBA

    Maybe Its Just Not Possible, But Then Again What Is Impossible

  15. #15
    Lively Member
    Join Date
    Aug 2000
    Location
    Darlington, United Kingdom
    Posts
    121
    Hmmm..

    I changed my Code to this

    VB Code:
    1. Sub DateCheck()
    2.     Dim CellDates(), i As Integer, j As Integer
    3.     'Assuming Cell A1 is the start of this range of dates and we do not know
    4.     'the last cell but the cell after the last cell is empty
    5.     Range("B7").Select
    6.     While ActiveCell.Value <> ""
    7.         i = i + 1
    8.         ActiveCell.Offset(1, 0).Select
    9.     Wend
    10.     'Now redim the CellDates Array so we can populate it with all values
    11.     ReDim CellDates(1 To i)
    12.     'place each cell into the array
    13.     Range("B7").Select
    14.     For j = 1 To i
    15.         CellDates(j) = "B" & j + 6
    16.     Next j
    17.     'Now loop through this array checking the values of the cell and changing the font to bold
    18.     'and the cell background to yellow
    19.     For j = 1 To i
    20.         If Date - CDate(Range(CellDates(j)).Value) > 60 Then
    21.             Range(CellDates(j)).Font.Bold = True
    22.             Range(CellDates(j)).Select
    23.             With Selection.Interior
    24.                 .ColorIndex = 6
    25.                 .Pattern = xlSolid
    26.                 .PatternColorIndex = xlAutomatic
    27.             End With
    28.         End If
    29.     Next j
    30. End Sub

    And it worked.. the spreadsheet that I am using has only 4 populated cells B6 to B9 with B6 Containing "DATE" and the remaining three cells contain the dates 1/1/01, 31/3/01, 1/6/01 respectively
    Do Not Underestimate The Power Of Simple VBA

    Maybe Its Just Not Possible, But Then Again What Is Impossible

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    Thank You DJDANNYK...your code did work, i forgot to chane the range address to B7.

    You have helped me immensely!!!!!!!!!

    I have also learned alot from you in this experience....

    Keep up the great work!!!!!!!!!!



  17. #17
    Lively Member
    Join Date
    Aug 2000
    Location
    Darlington, United Kingdom
    Posts
    121
    No problem..

    I prefer to put extra stuff into the code examples to help newbies learn additional stuff that they might not of thought of..

    Come back to me if you have any other questions with VBA for Excel... been using VBA for Excel for 3 years now, and I'm considered THE Guru around my workplace
    Do Not Underestimate The Power Of Simple VBA

    Maybe Its Just Not Possible, But Then Again What Is Impossible

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width