【发布时间】: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
【问题讨论】:
-
看看this post。这是解决问题的另一种方式
-
一般可以让用户输入国际格式的日期(
yyyy-mm-dd)。据我所知,这在两种环境中都应该得到很好的解释。 -
在 Access 中您可以Enter an ISO formatted date with input mask and full validation。不知道能不能被Excel采用。
标签: vba date input date-format date-formatting