Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在VBA中查找差异和比较日期_Vba_Date_Vba7 - Fatal编程技术网

在VBA中查找差异和比较日期

在VBA中查找差异和比较日期,vba,date,vba7,Vba,Date,Vba7,我试图编写一个代码,将日期作为输入,并检查它是否晚于代码中定义的日期 Sub times() Dim d1 As Date n = Application.InputBox("Enter a date in mm/dd/yyyy format: ") entered_date = CDate(entered_date) d1 = 5 / 11 / 2021 If d1 > entered_date Then d2 = DateDiff("D",

我试图编写一个代码,将日期作为输入,并检查它是否晚于代码中定义的日期

Sub times()
Dim d1 As Date
n = Application.InputBox("Enter a date in mm/dd/yyyy format: ")
entered_date = CDate(entered_date)
d1 = 5 / 11 / 2021
If d1 > entered_date Then
   d2 = DateDiff("D", d1, entered_date)
   MsgBox ("late by " & d2)
Else
   MsgBox ("on time")
   End If
End Sub
date diff函数似乎不适合我,或者我的逻辑有问题。
提前谢谢

这是我的答案。正如@NicholasHunter所说,
选项显式
总是更好! 很明显,你可以找出答案,但是考虑一下这个答案,不管怎样,希望能帮助其他人。
Option Explicit

Sub times()
    'Here you can validate the format date type for your system...
    Dim SystemDateType As Integer
    Dim SysFormatDate As String
    '   0 = m-d-y
    '   1 = d-m-y
    '   2 = y-m-d
    If Application.International(xlDateOrder) = 0 Then
        SystemDateType = 0
        SysFormatDate = "mm/dd/yyyy"
    ElseIf Application.International(xlDateOrder) = 1 Then
        SystemDateType = 1
        SysFormatDate = "dd/mm/yyyy"
    ElseIf Application.International(xlDateOrder) = 2 Then
        SystemDateType = 2
        SysFormatDate = "yyyy/mm/dd"
    End If
    'Of course you can do this:
    'SystemDateType = Application.International(xlDateOrder)
    'Or just use a Select Case...
    'But just want to be clear and set the variable SysFormatDate
    
    Dim StopedByUser As String: StopedByUser = "StopedByUser"
    'Here you can define your own message.
    
    Dim d1 As Date
    'Look for the DateSerial function
    
    Dim d2 As Variant
    'This could be Long, but just want to set a data type
    Dim ErrHandler As Integer
    Dim n As Variant
    'Or maybe String?
    
    Dim entered_date As Date
    'alwayes respect Data Types...
RetryInput:
    n = Application.InputBox("Enter a date in " & SysFormatDate & " format: ")
    'The user input...
    
    'Cancel...
    If n = False Then
        MsgBox StopedByUser
        End
    End If
    
    'Error Handler!
    If IsDate(n) Then 'If the user input a real date...
        entered_date = CDate(entered_date)
        d1 = DateSerial(2011, 11, 16) ' ==> YYYY, MM, DD
        'It is always better to use DateSerial!
        'And hard coded... Well hope is just your for the question.
        'd1 = 5 / 11 / 2021
        
        If d1 >= entered_date Then
            'The >= and <= are better in this case...
           d2 = DateDiff("D", d1, entered_date)
           MsgBox ("late by " & Abs(d2)) 'Abs return the absolute value, instead -321 return 321.
        Else
           MsgBox ("on time")
        End If
    Else    'If the user don't know what is a date...
        'ask the user... want to try again...
        ErrHandler = MsgBox("Please enter a formated date (" & SysFormatDate & ")", vbDefaultButton1 + vbYesNo, "Retry")
        
        If ErrHandler = 7 Then '7 is NO
            MsgBox StopedByUser
            End
        ElseIf ErrHandler = 6 Then '6 is YES and go back in the code to...
            GoTo RetryInput
        End If
        'Check for MSGBOX here: https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/msgbox-function
    End If
End Sub
选项显式
次时报()
'您可以在此处验证系统的格式日期类型。。。
Dim SystemDateType为整数
Dim SysFormatDate作为字符串
'0=m-d-y
'1=d-m-y
'2=y-m-d
如果Application.International(xlDateOrder)=0,则
SystemDateType=0
SysFormatDate=“mm/dd/yyyy”
ElseIf Application.International(xlDateOrder)=1然后
SystemDateType=1
SysFormatDate=“dd/mm/yyyyy”
ElseIf Application.International(xlDateOrder)=2然后
SystemDateType=2
SysFormatDate=“yyyy/mm/dd”
如果结束
“当然,你可以这样做:
'SystemDateType=Application.International(xlDateOrder)
'或者只使用选定的案例。。。
'但只想澄清一下,并设置变量SysFormatDate
Dim StopedByUser作为字符串:StopedByUser=“StopedByUser”
'在这里,您可以定义自己的消息。
日期为d1
'查找DateSerial函数
Dim d2作为变体
'这可能很长,但只想设置一个数据类型
作为整数的Dim ErrHandler
作为变体的dimn
“或者是绳子?
Dim输入的日期为日期
'始终尊重数据类型。。。
重试输入:
n=Application.InputBox(“以“&SysFormatDate&”格式输入日期:”)
'用户输入。。。
“取消。。。
如果n=False,则
MsgBox StoppedByUser
终点
如果结束
'错误处理程序!
如果是IsDate(n),则“如果用户输入实际日期。。。
输入日期=日期(输入日期)
d1=日期序列(2011年11月16日)==>YYYY、MM、DD
“最好使用DateSerial!
“而且是硬编码的。。。希望就是你对这个问题的答案。
'd1=2021年11月5日
如果d1>=输入的日期,则

“>=和d1=5/11/2021意味着d1得到5除以11除以2021的值。如何将其设置为2021年5月5日?我尝试了#5/11/2021#,但它不起作用。这个问题是一个很好的例子,说明了为什么您应该始终将选项显式放在每个代码模块的顶部。
entered\u date
将始终是代码中的
0
,除非它在其他地方定义为全局变量或其他任何内容。您应该将
n
替换为
entered\u date
。你说我试过(2021年11月5日),但没有成功是什么意思?对我来说,它奏效了#