VBA日期校验(年月日,年月,年)
首先某一个日期的text的Exit事件



Function CheckDate(DateString As String) As Boolean
CheckDate = False
If Len(DateString) < 4 Then
CheckDate = False
Exit Function
End If
If Len(DateString) = 4 Then
Dim yyyy As Integer
yyyy = Val(Left(DateString, 4)) '擭(YYYY 4寘)
CheckDate = True
Exit Function
End If
If Len(DateString) = 6 Then
Dim yyyy2 As Integer
yyyy2 = Val(Left(DateString, 4)) '擭寧(YYYYMM 6寘)
Dim mm2 As Integer
mm2 = Val(Mid(DateString, 5, 2))
If mm2 > 0 And mm2 < 13 Then
CheckDate = True
Exit Function
End If
End If
If Len(DateString) = 8 Then
Dim yyyy1 As Integer
yyyy1 = Val(Left(DateString, 4)) '擭寧擔(YYYYMMDD 8寘)
Dim mm1 As Integer
mm1 = Val(Mid(DateString, 5, 2))
Dim dd As Integer
dd = Val(Right(DateString, 2))
If mm1 > 0 And mm1 < 13 Then
If dd > 0 Then
Select Case mm1
Case 2
If (yyyy1 Mod 4) <> 0 Or (yyyy1 Mod 100) = 0 And (yyyy1 Mod 400) <> 0 Then
If dd < 29 Then
CheckDate = True
Exit Function
Else
CheckDate = False
Exit Function
End If
End If
If dd < 30 Then
CheckDate = True
Exit Function
Else
CheckDate = False
Exit Function
End If
Case 4, 5, 9, 11
If dd < 31 Then
CheckDate = True
Exit Function
Else
CheckDate = False
Exit Function
End If
Case Else
If dd < 32 Then
CheckDate = True
Exit Function
Else
CheckDate = False
Exit Function
End If
End Select
End If
End If
End If
End Function
这篇博客介绍了如何在VBA中进行日期校验,包括年月日、年月和年的格式检查,特别关注了日期文本框的Exit事件处理。
5962

被折叠的 条评论
为什么被折叠?



