'DateAdd function shows date from 1900 in VBA
I have run to this weird problem and I really can't understand what is happening.
I have the code below
VBA Code:
Dim tddate As Date
Dim expdate As String
Dim inputdate As String
tddate = Worksheets("Sheet3").Range("pdate").Value
inputdate = TextBox1.Value
expdate = Format(DateAdd("m", 18, tddate), "dd/mm/yyyy")
If inputdate = expdate Then
Range("A1").Value = TextBox1.Value
Else
MsgBox "Input the correct date"
Cancel = True
End If
It compares the user input with the date that MUST be in a textbox...Until yesterday it worked PERFECT with no problems at all
Today that I continued my work on the excel project If I Debug.Print the expdate varieable the date that it shows is 30/06/1901 !!!!!!
PS. If I Debug.Print Now it shows the correct date PS.2. I am using Excel 2019 but I tried the workbook on a different laptop with Excel 2016 and shows the same thing. (The second try is because I changed my RAM yesterday -every time in my system is correct - and thought I messed up something)
Solution 1:[1]
Try working with true date values, not text:
Dim tddate As Date
Dim expdate As Date
Dim inputdate As Date
tddate = Worksheets("Sheet3").Range("pdate").Value
If IsDate(TextBox1.Value) Then
inputdate = DateValue(TextBox1.Value)
expdate = DateAdd("m", 18, tddate)
If DateDiff("d", inputdate, expdate) = 0 Then
Range("A1").Value = inputdate
Else
Cancel = True
End If
Else
Cancel = True
End If
If Cancel Then
MsgBox "Input the correct date"
End If
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gustav |
