2012-02-04 41 views
1

我需要檢查在文本框中輸入的日期是否有效。它必須是單個文本框,所以沒有這種方法。 現在,我有這樣的代碼:來自文本框和倒數日/月值的數據控制

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 

很抱歉,如果它在意大利的文本。該功能正常工作,唯一的問題是,如果我輸入例如11/14/12(日期是dd/mm/yy,14是錯誤類型),它會反轉日期和月份值。相反,我希望sub告訴用戶再次檢查他的輸入!你可以幫我嗎?謝謝!

+0

什麼是調用arrayList調用?你能提供你的工作表的例子嗎? – Raystafarian 2012-02-04 13:45:15

+0

即使必須是單個文本框,也可以使用彈出的日期選擇器來絕對控制結果 – datatoo 2012-02-04 16:56:09

回答

0

這個問題每個月左右都有變化。我確信Excel會將一個有效的美國日期作爲美國日期。我已經想了很多年,但其他人不同意。

我使用類似下面的函數檢查格式,我相信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