Reputation: 307
I'm trying to write a code on cells that have both dates and times in them (Example: 1/29/14 9:15 AM). I'm trying to see if the date from 1 cell matches the date on another cell. How could I do that?
Example:
A1: 2/13/14 B1: 2/13/14 - This would return True
A2: 1/15/14 B2: 4/25/14 - This would return false
Any way I could achieve this?
Thanks
Upvotes: 1
Views: 2690
Reputation: 24237
Excel stores dates as fractional numbers where the whole number represents the date and the fraction represents the time of day (e.g., 0.5 = noon). The whole number is the number of days since 1/1/1900. So Excel stores 1/3/1900 6:00 PM
internally as 3.75
.
As long as they are formatted as dates, you can compare them directly:
C1 formula: =(A1=B1)
C2 formula: =(A2=B2)
To ignore the time portion of the date, take the integer portion only:
C1 formula: =(INT(A1)=INT(B1))
C2 formula: =(INT(A2)=INT(B2))
There's no real need for VBA, but if you want to compare the dates in VBA, the same rules apply:
Sub CompareDates()
With ActiveSheet
.[C1] = (Int(.[A1]) = Int(.[B1]))
.[C2] = (Int(.[A2]) = Int(.[B2]))
End With
End Sub
Upvotes: 2