Home > Back-end >  If time hasn't happened yet, exit sub
If time hasn't happened yet, exit sub

Time:06-22

I have a macro that pulls historic data from a company website. I noticed that the logic of the entire tool breaks when someone pulls time that has not yet happened, (pulls data for tomorrows shift, today) for example.

Anyone have an example of some sort of if statement that ensures the time is, in fact, in the past. And that they are not time traveling.

CodePudding user response:

You can compare your date to =NOW()

Sub mysub()

Dim CurrentDate As Date
CurrentDate = Now()

Dim iYear As Integer: iYear = 2035
Dim iMonth As Integer: iMonth = 10
Dim iDay As Integer: iDay = 25
Dim dt As Date
dt = DateSerial(Year:=iYear, Month:=iMonth, Day:=iDay)

If dt > CurrentDate Then
    MsgBox ("dt is in the future")
End If

End Sub
  • Related