Problem with Comparing Two Dates in VBA

rkaczano

Board Regular
Joined
Jul 24, 2013
Messages
141
Office Version
  1. 365
Platform
  1. Windows
I have a Date value that when tested as = another Date Value should = TRUE as they are the same date. But is is coming up as FALSE and I cannot figure out why. I am trying to figure out if this has something to with variable declarations or whether I am comparing a formatted date to a variable. I have wrapped both dates in CDbl() to see if that helps and the problem still persists.

I set the initial date and comparison date in an array as follows:

Public currentdate As Date
currentdate = DateAdd("h", 0, OldDate)
And Debug.Print currentdate returns "43831"


Later in the routine I add an hour to currentdate and I also add an hour to a date in array called arr using two different approaches: DateAdd() Function and; + 1/24. The two results look exactly the same. But the LEN() function tells a different story. Now to be clear, the value in arr is being added with delimiters which I take out when doing the comparison and I am wondering if that defaults it to a text variable and I am wondering if this is causing the difference in the LEN() results.

currentdate = DateAdd("h", 1, OldDate)
Debug.Print currentdate returns " 43831.0416666667 "
CDbl(arr(0)) + 1 / 24
Debug.Print CDbl(arr(0)) returns " 43831.0416666667 "
?LEN(currentDate) returns 8
?LEN(arr(0)) returns 16

And not surprisingly, when I run a test in an IF stmt to see if the values are the same I get FALSE as a response
If CDbl(currentdate) = CDbl(arr(0)) Then.....(this returns FALSE which is not what I want)

How do I convert currentdate to a value that makes it equal the value in arr(0)? And I need the decimal points in both numbers.

Thanks
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I even tried converting both variables to using CDate() during the comparison but this does not work either:.

?CDate(CurrentDate)
1/1/2020 1:00:00 AM

?CDate(currenthouroutagestartdate(0))
1/1/2020 1:00:00 AM

?CDate(currentdate) = CDate(currenthouroutagestartdate(0))
False
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,094
Latest member
teemeren

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