Compare two dates

USAMax

Well-known Member
Joined
May 31, 2006
Messages
846
Office Version
  1. 365
Platform
  1. Windows
A1 = 12/30/2012
B1 = 12/30/2012
B2 = 1/5/2013

Can someone tell me why 01/05/2013 is not larger then 12/30/2012?

code
DateOne = Format(Range("A1"), "mm/dd/yyyy"
DateTwo = Format(Range("B1"), "mm/dd/yyyy"
DateThr = Format(Range("B2"), "mm/dd/yyyy"
Chk1 = DateOne <= DateTwo
Chk2 = DateThr > DateTwo
code

Results:
Chk1 = True
Chk2 = False

I have not worked with dates for a long time.
Please, can anyone help?
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Because the FORMAT function returns a TEXT string.
So you're doing a greater than / less than comparison on Text Strings, not Numerical Dates.

This will give pretty much same results

Rich (BB code):
x = 100
y = 50
chk1 = x > y '<-Returns True

x = Format(x, "000")
y = Format(y, "00")
chk2 = x > y '<-Returns False
 
Last edited:
Upvote 0
Someone suggested defining them as Double and then reading them in and this worked for two of them.

A1 is actual from from an imported workbook that was exported from another program! What a mess! When I try to import the cell's value into a Double variable I get a type mismatch.
 
Upvote 0
How about
Code:
Dim DateOne As Date, DateTwo As Date, DateThr As Date

DateOne = Range("A1").Value
DateTwo = Range("B1").Value
DateThr = Range("B2").Value

Chk1 = DateOne <= DateTwo
Chk2 = DateThr > DateTwo
 
Upvote 0
How about
Code:
Dim DateOne As Date, DateTwo As Date, DateThr As Date

DateOne = Range("A1").Value
DateTwo = Range("B1").Value
DateThr = Range("B2").Value

Chk1 = DateOne <= DateTwo
Chk2 = DateThr > DateTwo


Ya, now that I wrote a complicated a big complicated work-around you come up with this simple solution. LOL

Thank you, this will work much better!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top