VBA高级输入框日期

VBA高级输入框日期,vba,excel,Vba,Excel,我的代码工作得非常好,但是,我发现当用户输入dd/mm/yy时,它被接受,而它不应该被接受,如果用户不输入mm/dd/yyyy,我如何捕获用户 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")

我的代码工作得非常好,但是,我发现当用户输入dd/mm/yy时,它被接受,而它不应该被接受,如果用户不输入mm/dd/yyyy,我如何捕获用户

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
Dim LastReportDate作为字符串
暗起始日期为字符串
重试:
LastReportDate=InputBox(“以mm/dd/yyyy格式插入日期”,“已验证的日期范围:”,格式(现在(),“mm/dd/yyyy”))
'用户按“取消”
如果strpr(LastReportDate)=0,则MsgBox“按下取消按钮,数据范围已验证-范围(B3)将为空”。vbCritical,“用户取消日期输入”:退出子系统
'如果用户未在0处输入anthying重新启动
如果LastReportDate=vbNullString且StrPtr(LastReportDate)>0,则MsgBox“您没有输入字符串”:转到重试
'如果用户确实输入了日期,则去验证它
如果LastReportDate vbNullString,则:转到ValidateDateFormat
验证数据格式:
'用户输入了有效日期
如果是IsDate(LastReportDate),则
'在B3中输入开始日期-结束日期
'注意开始日期=-6天:数据范围验证为一周
此工作簿。工作表(1)。单元格(3,2)。值=日期添加(“d”、-6,LastReportDate)&“-”和LastReportDate
'如果用户输入了错误的日期格式,则转到0
其他的
MsgBox“错误的日期格式”:转到重试
如果结束
将LastReportDate设置为字符串
暗起始日期为字符串
重试:
LastReportDate=InputBox(“以mm/dd/yyyy格式插入日期”,“已验证的日期范围:”,格式(现在(),“mm/dd/yyyy”))
'用户按“取消”
如果strpr(LastReportDate)=0,则MsgBox“按下取消按钮,数据范围已验证-范围(B3)将为空”。vbCritical,“用户取消日期输入”:退出子系统
'如果用户未在0处输入anthying重新启动
如果LastReportDate=vbNullString且StrPtr(LastReportDate)>0,则MsgBox“您没有输入字符串”:转到重试
'如果用户确实输入了日期,则去验证它
如果LastReportDate vbNullString,则:转到ValidateDateFormat
验证数据格式:
'如果用户未输入所需格式,则捕获用户
“谢谢@cyboashu
如果格式为(LastReportDate,“mm/dd/yyyy”)LastReportDate,则
MsgBox“错误的日期格式”:转到重试
'用户输入了有效日期
ElseIf IsDate(最后报告日期)然后
'在B3中输入开始日期-结束日期
'注意开始日期=-6天:数据范围验证为一周
此工作簿。工作表(1)。单元格(3,2)。值=日期添加(“d”、-6,LastReportDate)&“-”和LastReportDate
'如果用户输入了错误的日期格式,则转到0
其他的
如果结束
MsgBox“CFR宏已完成”,vbInformation+vbOKOnly,“宏状态:成功”
此工作簿.工作表(1).范围(“B4”)=格式(现在为“mm/dd/yyyy”)
此工作簿。工作表(1)。范围(“B2”)。选择
将LastReportDate设置为字符串
暗起始日期为字符串
重试:
LastReportDate=InputBox(“以mm/dd/yyyy格式插入日期”,“已验证的日期范围:”,格式(现在(),“mm/dd/yyyy”))
'用户按“取消”
如果strpr(LastReportDate)=0,则MsgBox“按下取消按钮,数据范围已验证-范围(B3)将为空”。vbCritical,“用户取消日期输入”:退出子系统
'如果用户未在0处输入anthying重新启动
如果LastReportDate=vbNullString且StrPtr(LastReportDate)>0,则MsgBox“您没有输入字符串”:转到重试
'如果用户确实输入了日期,则去验证它
如果LastReportDate vbNullString,则:转到ValidateDateFormat
验证数据格式:
'如果用户未输入所需格式,则捕获用户
“谢谢@cyboashu
如果格式为(LastReportDate,“mm/dd/yyyy”)LastReportDate,则
MsgBox“错误的日期格式”:转到重试
'用户输入了有效日期
ElseIf IsDate(最后报告日期)然后
'在B3中输入开始日期-结束日期
'注意开始日期=-6天:数据范围验证为一周
此工作簿。工作表(1)。单元格(3,2)。值=日期添加(“d”、-6,LastReportDate)&“-”和LastReportDate
'如果用户输入了错误的日期格式,则转到0
其他的
如果结束
MsgBox“CFR宏已完成”,vbInformation+vbOKOnly,“宏状态:成功”
此工作簿.工作表(1).范围(“B4”)=格式(现在为“mm/dd/yyyy”)
此工作簿。工作表(1)。范围(“B2”)。选择

也比较日期格式,
如果格式(LastReportDate,“mm/dd/yyyy”)=LastReportDate,则
如果LastReportDate vbNullString,则:转到ValidateDateFormat
-指令分隔符是多余的/混乱的,
ValidateDateFormat
标签运行,无论您是否跳转。此外,考虑制作自定义用户窗体而不是<代码>输入框< /代码>;您将能够更好地控制验证和一切。。。并考虑一个更好的循环策略,而不是<代码> Goto Sub-TrapUser():Do:Loop:End-Sub也比较日期格式,
如果格式(LastReportDate,“mm/dd/yyyy”)=LastReportDate,则,无论您是否跳转,都会运行
ValidateDateFormat
标签。此外,考虑制作自定义用户窗体而不是<代码>输入框< /代码>;您将能够更好地控制验证和一切。。。并考虑一个更好的循环策略,而不是<代码> Goto Sub-TrapUser():Do:Loop:End-Sub
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:

'Trap user if the user did not enter the required format
'Thanks @cyboashu
If Format(LastReportDate, "mm/dd/yyyy") <> LastReportDate Then
MsgBox "Wrong Date Format": GoTo reTry

'User Entered a Valid Date
ElseIf 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
End If

MsgBox "CFR Macro Completed.", vbInformation + vbOKOnly, "Macro Status: Successful"
ThisWorkbook.Worksheets(1).Range("B4") = Format(Now, "mm/dd/yyyy")
ThisWorkbook.Worksheets(1).Range("B2").Select