【问题标题】:Finding difference and Comparing dates in VBA在 VBA 中查找差异和比较日期
【发布时间】:2021-04-04 15:21:21
【问题描述】:

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

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

日期差异功能似乎对我不起作用,或者我的逻辑有问题。 提前致谢!

【问题讨论】:

  • d1=5/11/2021 表示 d1 得到 5 除以 11 除以 2021 的值。
  • 如何设置为 2021 年 5 月 5 日?
  • 我尝试了#5/11/2021#,但没有成功
  • 这个问题是一个很好的例子,说明为什么您应该始终将 Option Explicit 放在每个代码模块的顶部。
  • entered_date 在代码中将始终为 0 ,除非它在其他地方定义为全局变量或其他。您应该将n 替换为entered_date我试过#5/11/2021#,但没有用是什么意思?对我来说它有效。 #

标签: vba date vba7


【解决方案1】:

这就是我的答案。正如@NicholasHunter 所说,Option Explicit 总是更好! 明明自己想通了,不过考虑一下这个答案,反正希望能帮到别人。

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

【讨论】:

    猜你喜欢
    • 2021-09-23
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多