【发布时间】:2018-02-01 08:41:41
【问题描述】:
我正在尝试制作一个宏,该宏将用户输入的截止日期输入单元格E2,然后检查与今天的日期相比,截止日期是否已过。此外,我试图在单元格F2 中显示计数,显示距离截止日期还有多少天,或者超过截止日期多少天。我有以下代码,但它没有正确计算天数,我不确定截止日期检查是否正常:
Sub Test()
Dim DDate As Date
Dim Ndate As String
Dim DDays As String
Dim NDays As String
Dim LDays As String
Ndate = Format(Date, "yyyymmdd") 'current date
DDate = Range("E2") 'due date, as entered in the cell by user, 20180101 in this example)
If DDate < Ndate Then Range("G1") = "ERROR" 'calculate whether the due date (DDate) has passed, and do something if yes)
NDays = Format(Date, "dd")
DDays = Range("E2").Value.Format(Date, "dd")
LDays = NDays - DDays 'count how many days are left until the due date (or how many days it's over the due date)
Range("F2") = LDays 'put the number of days in cell F2
End Sub
【问题讨论】:
-
为什么需要 VBA? Excel 的本机电子表格功能可以完成这项工作。在 F2 中,输入 =E2-TODAY()。