【问题标题】:Check date value depending on country specific format根据国家/地区特定格式检查日期值
【发布时间】:2021-01-12 17:54:26
【问题描述】:

我想让用户在 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

【问题讨论】:

标签: vba date input date-format date-formatting


【解决方案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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多