[Solved] Excel VBA code to compare system date with date in the worksheet


I put together a small procedure to get you started. It assumes the date/time in the worksheet is in cell A1. If the system date is the same as the date in cell A1, it then compares the system time with the time in cell A1. If they are the same to the minute then nothing happens.

However, if they are different then the number of minutes by which they are different appears in cell B1.

Here is the code:

Public Sub DateTimeCheck()

    Dim d  As Double
    Dim s1 As String
    Dim s2 As String

    s1 = Format([a1], "dd-mm-yyyy hh:mm")
    s2 = Format(Now, "dd-mm-yyyy hh:mm")

    If Left$(s2, 10) = Left$(s1, 10) Then
        d = TimeValue(Right$(s2, 5)) - TimeValue(Right$(s1, 5))
        If d Then
            [b1] = d * 1440
        End If
    End If

End Sub

Now, if you have more cells with dates in column A and you want to compare them as well, then you will need to modify this code so that it “processes” all of them.

As far as writing the difference in minutes to the email… we would need much more detail for that!

2

solved Excel VBA code to compare system date with date in the worksheet