Link to home
Start Free TrialLog in
Avatar of RWayneH
RWayneHFlag for United States of America

asked on

Active Cell = Today ?

I am trying to check the value of a cell, to see if it is equal to today date or not?

Format of the ActiveCell is:

  : StartTimeStamp : #5/1/2017 7:57:12 AM# : Variant/Date

Here is what I have so far.  How do I get this to a True or Yes?  Keeps returning False/No.   Any ideas?

Sub Macro2()
'
    Sheets("TaskTracker").Select
    Range("B1000").Select  'No days task items should ever reach 1000 lines
    Selection.End(xlUp).Select

    StartTimeStamp = ActiveCell.Value
    
    Do While Not StartTimeStamp = Now()
        StartTimeStamp = ActiveCell.Value
        MsgBox ("No")
        ActiveCell.Offset(-1, 0).Select
    Loop

End Sub

Open in new window

Avatar of Roy Cox
Roy Cox
Flag of United Kingdom of Great Britain and Northern Ireland image

It's not clear what you are asking. It looks like your active cell does not contain a date, but is made up of text & a date.

If the cell actually contains a date then try this

Option Explicit

Sub Checkdate()
If Not IsDate(ActiveCell) Then Exit Sub
MsgBox ActiveCell.Value = Date
End Sub

Open in new window

Avatar of RWayneH

ASKER

It returns FALSE in the MsgBox, so ActiveCell appears to not be a date..

So when I name StartTimeStamp, it is not actually returning a date. (text?)  or I need to convert it to date so it can be identified as one.
Not sure I can do that...  grab whatever is in the cell, convert it so I can tell..  My goal was to call to a Sub if it is not todays date.

I do some calculations on the date value, to get durations, so it is working as a number in the formulas...  then I copy and paste values when I am done.  That is how I am getting the value of StartTimeStamp.  It was a formula, now it is a pasted value of what the formula was.

 : StartTimeStamp : #5/1/2017 7:57:12 AM# : Variant/Date
SOLUTION
Avatar of Flyster
Flyster
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RWayneH

ASKER

Flyster:   Format(StartTimeStamp ,"mm/dd/yyyy hh:mm")      is expecting an = ??    if removing the seconds truly works.

I need to check/convert the cells one at a time, cannot do the whole column...  How would I fit this into my example?
Avatar of RWayneH

ASKER

What is actually in the cell?   #5/1/2017 7:57:12 AM#   well without the #s

 : StartTimeStamp : #5/1/2017 7:57:12 AM# : Variant/Date
I keep asking that myself! It could be a combination of Text and date or the text could actually be a Custom Format. It will make a great difference to how it is coded.
RWayneH,

Do you want Msgbox to display yes or no if cell value is today's date from B1000 to B1 or B2?

If yes, then you would be clicking OK on Msgbox, 1000 times?

I would advice you should have adjacent cell in adjacent column to update Yes/No, if any cell in Col B has today's date, doesn't matter format.

Try below with my proposal:
Sub UpdateRemarks()
Dim Ws As Worksheet
Dim LR As Long
Dim i As Integer
Set Ws = Worksheets("TaskTracker")
LR = Ws.Range("B" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
For i = 2 To LR
    If Day(Ws.Cells(i, 2)) = Day(Date) And Month(Ws.Cells(i, 2)) = Month(Date) And Year(Ws.Cells(i, 2)) = Year(Date) Then
        Ws.Cells(i, 3).Value = "Yes"
    Else
        Ws.Cells(i, 3).Value = "No"
    End If
Next i
Application.ScreenUpdating = True
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You may as well use a formula or Conditional formatting instead of VBA. The code  which will fail if the cell is not a date.
Wayne, you don't need to format the cell or select. Excel stores dates and times as a number representing the number of days since 1900-Jan-0, plus a fractional portion of a 24 hour day: ddddd.tttttt . This is called a serial date, or serial date-time. The integer portion of the number, ddddd, represents the number of days since 1900-Jan-0. The formatting is only so that we humans recognise the dates.

The most important thing is knowing what the author has entered in the cells, is it a true date, or date and time or is a mix with text.

This will work with a date or date and time in a cell, it will

MsgBox DateSerial(Year(ActiveCell), Month(ActiveCell), Day(ActiveCell)) = Date

Open in new window

Dates.xlsm
Avatar of RWayneH

ASKER

I figured it out..  Thanks for the help.