简体   繁体   English

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

[英]Now() function in Excel VBA macro stopped working

I'm trying to set up a Warning in a prompt that I'm using in a VBA Command button in an Excel workbook. 我正在尝试在Excel工作簿的VBA命令按钮中使用的提示中设置警告。

It worked well two days ago, but it does not work at the present moment and I do not understand why. 两天前它运行良好,但目前不起作用,我不明白为什么。 Here you have the code that was working before: 在这里,您可以使用之前的代码:

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

This is the part that was working before, but stopped working yesterday: 这是以前工作的部分,但昨天停止工作:

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

At the present moment, every time I introduce any date into this prompt screen, no matter if it is later or earlier than the current date I get the Warning "The date you entered is LATER THAN THE CURRENT DATE!!!". 目前,每次我在此提示屏幕中输入任何日期时,无论该日期晚于还是早于当前日期,我都会收到警告“您输入的日期晚于当前日期!!”。

The only thing that has changed in the Excel Workbook is that there are several cells across the different spreadsheets that contain future dates from future appointments, but the function now() should check the system date, not the dates in the workbook, doesn't it? Excel工作簿中唯一发生变化的地方是,不同电子表格中有多个单元格包含将来约会中的将来日期,但是now()函数应检查系统日期,而不是工作簿中的日期,它?

Thank you very much, 非常感谢你,

Best Regards, 最好的祝福,

Yatrosin Yatrosin

Your problem is that you are comparing a string entered by the user with a date returned by the now function. 您的问题是您正在将用户输入的字符串与now函数返回的日期进行比较。

This values are of two different types. 该值有两种不同的类型。

If FinalDate > Now Then

You should convert FinalDate to a Date with CDate() 您应该使用CDate()将FinalDate转换为Date

if CDate(FinalDate) > Now Then

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM