ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   comparing dates (https://www.excelbanter.com/excel-programming/313077-comparing-dates.html)

Myriam

comparing dates
 
I'm trying to test for a specific date.
If todays date is greater than the date in cell A1
then complete the action, else, show the message "Unable to ..."
but unless I use < the comparison does not work. The code
does not run. Why is this?
The following runs:
-------------
Dim dt as Date
dt = .Cells (1,1)
If date1 < Now() Then
MsgBox "Unable to.."
------------
This does not run:

Dim dt as Date
dt = .Cells (1,1)
If dt Now() Then
MsgBox "Unable to.."
----------
Thanks in advance.





Rob van Gelder[_4_]

comparing dates
 
In your 2nd example you're using different names for your variables. dt vs.
date1

Always use Option Explicit.
You can use Date instead of Now which returns just date (not time hh:mm:ss)

Sub test()
Dim dtm As Date

dtm = Cells(1, 1).Value
If dtm <= Date Then
MsgBox "Unable to run"
End If
End Sub


--
Rob van Gelder - http://www.vangelder.co.nz/excel


"Myriam" wrote in message
...
I'm trying to test for a specific date.
If todays date is greater than the date in cell A1
then complete the action, else, show the message "Unable to ..."
but unless I use < the comparison does not work. The code
does not run. Why is this?
The following runs:
-------------
Dim dt as Date
dt = .Cells (1,1)
If date1 < Now() Then
MsgBox "Unable to.."
------------
This does not run:

Dim dt as Date
dt = .Cells (1,1)
If dt Now() Then
MsgBox "Unable to.."
----------
Thanks in advance.







Bill Kuunders

comparing dates
 
This works.........

Sub check()
Dim dt As Date
dt = Cells(1, 1)
If dt Now() Then
MsgBox "Unable to.."
End If
End Sub

Regards
Bill K

"Myriam" wrote in message
...
I'm trying to test for a specific date.
If todays date is greater than the date in cell A1
then complete the action, else, show the message "Unable to ..."
but unless I use < the comparison does not work. The code
does not run. Why is this?
The following runs:
-------------
Dim dt as Date
dt = .Cells (1,1)
If date1 < Now() Then
MsgBox "Unable to.."
------------
This does not run:

Dim dt as Date
dt = .Cells (1,1)
If dt Now() Then
MsgBox "Unable to.."
----------
Thanks in advance.







Myriam

comparing dates
 
Thanks, Rob.
I was missing .Value
Regards,

"Rob van Gelder" wrote:

In your 2nd example you're using different names for your variables. dt vs.
date1

Always use Option Explicit.
You can use Date instead of Now which returns just date (not time hh:mm:ss)

Sub test()
Dim dtm As Date

dtm = Cells(1, 1).Value
If dtm <= Date Then
MsgBox "Unable to run"
End If
End Sub


--
Rob van Gelder - http://www.vangelder.co.nz/excel


"Myriam" wrote in message
...
I'm trying to test for a specific date.
If todays date is greater than the date in cell A1
then complete the action, else, show the message "Unable to ..."
but unless I use < the comparison does not work. The code
does not run. Why is this?
The following runs:
-------------
Dim dt as Date
dt = .Cells (1,1)
If date1 < Now() Then
MsgBox "Unable to.."
------------
This does not run:

Dim dt as Date
dt = .Cells (1,1)
If dt Now() Then
MsgBox "Unable to.."
----------
Thanks in advance.









All times are GMT +1. The time now is 04:56 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com