Excel VBAmacros中的Now()函数停止工作

我正在尝试在Excel工作簿中的“VBA命令”button中使用的提示中设置“警告”。

两天前运作良好,但现在不行,我不明白为什么。 在这里你有以前工作的代码:

Option Explicit Private Sub CommandButton1_Click() Dim wb As Workbook Dim FinalDate As Variant Set wb = Application.Workbooks("Test") FinalDate = InputBox("Introduce the final date for analysis in the following format MM/DD/YYYY") '< Check that final date is in the correct format If IsDate(FinalDate) = False Then MsgBox ("The date you entered is NOT IN THE CORRECT FORMAT!!!") Exit Sub End If '<Check that final date is NOT later than today If FinalDate > Now Then '< **This part is the one that used to work** MsgBox ("The date you entered is LATER THAN THE CURRENT DATE!!!") Exit Sub End If wb.Worksheets("Sheet1").Range("K2") = FinalDate End Sub 

这是以前工作的部分,但昨天停止工作:

 If FinalDate > Now Then MsgBox ("The date you entered is LATER THAN THE CURRENT DATE!!!") Exit Sub End If 

目前,每当我在这个提示画面中引入任何date时,无论是晚于还是早于当前date,我都会收到警告“您input的date不超过当前date!!!”。

Excel工作簿中唯一改变的是在不同的电子表格中有几个单元格包含未来约会的未来date,但now​​()函数应该检查系统date,而不是工作簿中的date,而不是它?

非常感谢你,

最好的祝福,

Yatrosin

你的问题是,你正在比较用户input的string与now函数返回的date。

这个值有两种不同的types。

 If FinalDate > Now Then 

您应该使用CDate()将FinalDate转换为date

 if CDate(FinalDate) > Now Then