Home>

I want to judge whether the memorized year/month/day/hour/minute/second is properly reading the data of the next second.

There is no error message.
IF statement doesn't judge well.

Applicable source code
If data(i).date = DateAdd("s", 1, data(i-1).date) Then
     I = i + 1
     Else
     MsgBox "The data is incorrect."
         Unload Standbyscreen
         Exit Sub
      End If


(Only the problematic sentence is pasted)

What I tried

The solution is completely unknown and untouched...

If the year, month, day, hour, minute, and second that you have just memorized is equal to the value that has been memorized by adding 1 second to the previous date, proceed to the next value.
If not, the data is incorrect and the process is terminated.

The flow is as follows.

This IF statement has the same value on the left and right side, but it goes into the else statement, which is a problem. ..
All numbers are in this notation. 2020/3/1 7:18:04

What I don't understand about this error is that it makes more difference depending on the number of seconds.
In 2020/3/1 7:18:04, it goes to else, but in the next 2020/3/1 7:18:05, it moves normally.
2020/3/1 7:18:06 will move normally, and on 2020/3/1 7:18:07, it will move to else again.

Set the value you saw in the watch window.

Because the data is different, it is different from the above time.

Supplemental information (FW/tool ​​version, etc.)

Since the original data is a CSV file and is a string type, it is converted to a Date type when it is stored in data(i).date.
It is exactly the same when you look at the numbers inside using a debug tool.
Does it mean that they look the same, but they are actually different?

Thanks for your response...!

  • Answer # 1

    I checked it with the following code.
    Certainly it will be False when compared to the DateAdd result.

      Dim d1 As Date, d2 As Date
        d1 = #1/1/2020 12:45:30 PM#
        d2 = #1/1/2020 12:45:31 PM#
        Debug.Print d2 = DateAdd("s", 1, d1)

    I tried converting with CDate whether the return value of DateAdd is affected by Variant type, or storing the result in Date type variable and comparing it, it became False.

    The cause is a little unknown. It may be called a bug.

    As a solution, DateDiff was used to find the seconds difference, and then it returned True.

      Debug.Print DateDiff("S", d1, d2) = 1

  • Answer # 2

    I think that it is a specification that an arithmetic error of less than a few occurs, so
    How about comparing in Format?
    Format(data(i).Date, "yyyy/mm/dd hh:mm:ss") = Format(DateAdd("s", 1, data(i-a).Date), "yyyy/mm/dd hh :mm:ss")