1
我的代碼工作完全正常,但是,我發現,當用戶進入DD/MM/YY,所以當它不應該接受,我怎麼能捕獲用戶,如果用戶沒有輸入MM/DD/YYYY。VBA高級輸入框日期
Dim LastReportDate As String
Dim StartDate As String
reTry:
LastReportDate = InputBox("Insert date in format mm/dd/yyyy", "Date Range Validated:", Format(Now(), "mm/dd/yyyy"))
'User Presses "Cancel"
If StrPtr(LastReportDate) = 0 Then MsgBox "Cancel button pressed, Data Range Validated - Range (B3) will be blank.", vbCritical, "User Cancelled Date Entry": Exit Sub
'If user does not enter anthying restart at 0
If LastReportDate = vbNullString And StrPtr(LastReportDate) > 0 Then MsgBox "You entered no string": GoTo reTry
'If user did enter a date, then go validate it
If LastReportDate <> vbNullString Then: GoTo ValidateDateFormat
ValidateDateFormat:
'User Entered a Valid Date
If IsDate(LastReportDate) Then
'Put start date - end date in B3
'Note start date = -6 days: Data Range Validation is one week
ThisWorkbook.Worksheets(1).cells(3, 2).Value = DateAdd("d", -6, LastReportDate) & " - " & LastReportDate
'If user entered wrong date format then go to 0
Else
MsgBox "Wrong Date Format": GoTo reTry
End If
比較日期格式太,'如果格式(LastReportDate中, 「mm /日/年」)= LastReportDate Then' – cyboashu
'如果LastReportDate <> vbNullString然後:轉到ValidateDateFormat' - 指示分離器是冗餘/混亂,無論您是否跳轉,「ValidateDateFormat」標籤都會運行。另外,考慮製作一個自定義的UserForm而不是一個'InputBox';你會更好地控制驗證和一切。 ...並考慮比「GoTo」循環更好的循環策略。 –
@cyboashu,即作品完美,格式(LastReportDate中, 「mm /日/年」)<> LastReportDate,捕獲用戶僅輸入毫米/ dd/yyyy格式。 –