应用于 TextBox 值的 DateAdd 函数在 VBA 中显示 1900 年以来的日期

发布于 2025-01-10 22:05:30 字数 649 浏览 2 评论 0原文

我有代码可以将用户输入与文本框中必须包含的日期进行比较。

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

直到昨天它才起作用。

今天,如果我 Debug.Print expdate 变量,它会显示 30/06/1901。
如果我Debug.Print Now它会显示正确的日期。

我使用的是 Excel 2019。
我在另一台笔记本电脑上使用 Excel 2016 尝试了该工作簿,它显示了相同的内容。

I have code to compare user input with the date that must be in a textbox.

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

Until yesterday it worked.

Today if I Debug.Print the expdate variable it shows 30/06/1901.
If I Debug.Print Now it shows the correct date.

I am using Excel 2019.
I tried the workbook on a different laptop with Excel 2016 and it shows the same thing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人九事 2025-01-17 22:05:30

尝试使用真实的日期值,而不是文本:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文