微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

根据国家特定格式检查日期值

如何解决根据国家特定格式检查日期值

我想让用户在 InputBox 中输入日期。我将值加载到变量上并检查日期是否为可读格式(请参阅下面的代码)。
如果是这样,我想在后面的阶段用输入的日期在另一个表中输入行。

我在德国,我机器上的日期格式是 dd.mm.yyyy 所以我要求用户输入这种格式(见输入框)。

我在丹麦的同事的日期格式是 dd-mm-yyyy。
当他们以我的格式输入时,他们收到一条错误消息,指出日期格式不正确。
当他们输入的日期之间有一个减号时,稍后在表格中输入的日期有错误的值。

如何让代码在这两个国家都适用?

'msgBox for which period this information is supposed to be

Dim TheString As String,TheDate As Date
            
Do
    TheString = Application.InputBox("Please enter a date in the following format DD.MM.YYYY. Start with the first day of the month.")
    cd = Format(Day(TheString),0)
    
    If Not IsDate(TheString) Or cd <> 1 Then

        If MsgBox("You have entered the date in a wrong format. Do you want to try again selecting the date for these information?",vbCritical + vbYesNo,"DATE FORMAT INCORRECT") = vbNo Then
            MsgBox "The data transformation has been stopped.",vbinformation,"STOP TRANSFER"
            End
        End If
    
    Else

        TheDate = Format(DateValue(TheString),"DD.MM.YYYY")
    
    End If

Loop Until IsDate(TheString) And cd = 1

解决方法

感谢您的建议。我决定用年和月的两个输入框来简单地解决它,并使用 DateSerial 函数将日期放入相应用户的国家/地区格式中。

它似乎工作正常并且没有为进一步过滤造成问题:

YearInfo = Application.InputBox("Please enter the year you want to register the account data for. Use all four digits of the year.")    
MonthInfo = Application.InputBox("Please enter the month you want to register the account data for (as a number).")
    
TheDate = DateSerial(YearInfo,MonthInfo,1)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。