繁体   English   中英

比较日期时键入不匹配错误

[英]type mismatch error when comparing dates

该代码第二次通过For Next循环产生类型不匹配错误。 我不知道为什么

    Dim HD As Integer
    Dim HireDate2 As Date
    Dim HireDate3 As Integer
    Dim today As Date
    today = Now()
    HD = 2
    For HD = 2 To HireDate
        HireDate2 = Range("Z2:Z" & HD)
        MsgBox "Hire Date is " & HireDate2
       If (HireDate2 > today) Then
            HireDate3 = HireDate3 + 1
        End If
        HD = HD + 1
    Next HD

在此先感谢您的帮助

试试下面的代码:

Dim HD As Long
Dim HireDate2 As Date
Dim HireDate3 As Long
Dim MyToday As Date
Dim HireDate As Long

MyToday = Date  ' <-- get today's date into a variable

For HD = 2 To HireDate
    HireDate2 = Range("Z" & HD).Value
    MsgBox "Hire Date is " & HireDate2
    If DateDiff("d", MyToday, HireDate2) > 0 Then ' <-- if HireDate 2 is in the future
        HireDate3 = HireDate3 + 1
    End If
Next HD

暂无
暂无

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

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