数据控制从文本框和反转的日/月值

我需要检查在文本框中input的date是否有效。 它必须是一个单独的文本框,所以没有这样的解决方法。 现在,我有这个代码:

Private Sub cmdOK_Click() Dim dataAnalisi As Date If IsDate(txtDataAnalisi.Value) Then dataAnalisi = txtDataAnalisi.Value Dim giornoAnalisi, meseAnalisi As Integer giornoAnalisi = Format(dataAnalisi, "dd") meseAnalisi = Format(dataAnalisi, "mm") If giornoAnalisi <= 31 And meseAnalisi <= 12 Then Call arrayList(dataAnalisi) Unload Me Else GoTo DateError End If Else DateError: MsgBox "Inserire una data formattata correttamente!", vbCritical, "Errore nell'inserimento!" txtDataAnalisi.SetFocus End If End Sub 

对不起,如果它有意大利文。 该函数正常工作,唯一的问题是,如果我input例如11/14/12(date是dd / mm / yy,14是错误types),则会反转date和月份值。 相反,我希望子告诉用户再次检查他的input! 你可以帮我吗? 谢谢!

这个问题每个月都有变化。 我相信,Excel会把一个有效的美国date作为美国的date。 我已经想了很多年,但其他人不同意。

我使用下面的函数检查格式,我相信Excel会曲解并将其转换为明确的格式。

我用了几个月的英文缩写。 我相信法语是唯一不允许三个字符缩写几个月的语言,所以也许你有自己的一套。 你将不得不根据你的要求来调整这部分例程。

希望这有助于。

 Function MyDateValue(ByVal DateIn As String, ByRef DateOut As Date) As Boolean ' DateIn is a value to be checked as a valid date. ' If it is a valid date, DateOut is set to its value and the function ' returns True. ' Excel misinterprets dates such as "4/14/11" as 14 April 2011. This routine ' checks for such dates and, if necessary, changes them to an unambiguous ' format before calling IsDate and DateValue. Dim DatePart() As String Dim MonthNum As Long Const MonthAbbr As String = "janfebmaraprmayjunjulaugsepoctnovdec" ' Replace popular delimiters with Microsoft standard DateIn = Replace(DateIn, "-", "/") DateIn = Replace(DateIn, "\", "/") DatePart = Split(DateIn, "/") If UBound(DatePart) = 2 Then ' DateStg is three values separated by delimiters ' Check middle part If IsNumeric(DatePart(1)) Then MonthNum = Val(DatePart(1)) If MonthNum >= 1 And MonthNum <= 12 Then ' Middle part could be numeric month ' Convert to format Excel does not misinterpret 'Debug.Assert False DatePart(1) = Mid(MonthAbbr, ((MonthNum - 1) * 3) + 1, 3) DateIn = Join(DatePart, "-") If IsDate(DateIn) Then DateOut = DateValue(DateIn) MyDateValue = True Exit Function End If Else ' Middle part cannot be a month 'Debug.Assert False MyDateValue = False Exit Function End If Else 'Debug.Assert False ' The middle part is not a number. It could be a month abbreviation MonthNum = InStr(1, MonthAbbr, LCase(DatePart(1))) If MonthNum = 0 Then ' The middle portion is neither a month number nor a month abbreviation Debug.Assert False MyDateValue = False Else ' The middle portion is a month abbreviation. ' Excel will handle date correctly 'Debug.Assert False MonthNum = (MonthNum - 1) / 3 + 1 DateIn = Join(DatePart, "-") If IsDate(DateIn) Then 'Debug.Assert False DateOut = DateValue(DateIn) MyDateValue = True Exit Function End If End If End If Else ' Debug.Assert False ' Use IsDate for other formats If IsDate(DateIn) Then ' Debug.Assert False DateOut = DateValue(DateIn) MyDateValue = True Exit Function Else ' Debug.Assert False MyDateValue = False End If End If End Function