在用户表单中检查有效时间
Check Valid Time in Userform
我正在尝试在用户表单中进行错误检查,我希望能够检查输入的时间是否是有效的时间表达式(00:00 或 00 PM),因为我正在通过 TimeValue 命令发送它对于输出,以便时间标准化。如果输入的值不是有效表达式,我将显示错误消息并允许重新输入有效表达式。我不确定这是否是我可以使用 Regex 完成的事情,或者是否有更简单的选项。我在下面附上了我的代码。
Private Sub CommandButton_OK_Click()
Dim emptyrow As Long
'Error Check
If NameText.Value = "" Then
MsgBox "Please Enter Valid Name", vbOKOnly
Exit Sub
End If
'Make Sheet 1 Active
Sheet1.Activate
'Determine emptyRow
emptyrow = WorksheetFunction.CountA(Range("E:E")) + 1
'Convert Time of Inspection to Time Value
TimeText.Value = TimeValue(TimeText.Value)
'Transfer Info
Cells(emptyrow, 5).Value = DateText.Value
Cells(emptyrow, 6).Value = NameText.Value
Cells(emptyrow, 7).Value = ShiftText.Value
Cells(emptyrow, 8).Value = TimeText.Value
If CornNo.Value = True Then
Cells(emptyrow, 9).Value = "No"
Else
Cells(emptyrow, 9).Value = "Yes"
End If
If SurgeNo.Value = True Then
Cells(emptyrow, 10).Value = "No"
Else
Cells(emptyrow, 10).Value = "Yes"
End If
If MillNo.Value = True Then
Cells(emptyrow, 11).Value = "No"
Else
Cells(emptyrow, 11).Value = "Yes"
End If
If FBedNo.Value = True Then
Cells(emptyrow, 12).Value = "No"
Else
Cells(emptyrow, 12).Value = "Yes"
End If
If DDGOutNo.Value = True Then
Cells(emptyrow, 13).Value = "No"
Else
Cells(emptyrow, 13).Value = "Yes"
End If
If DDGInNo.Value = True Then
Cells(emptyrow, 14).Value = "No"
Else
Cells(emptyrow, 14).Value = "Yes"
End If
Unload Me
End Sub
看起来您可能希望使用 On Error GoTo Label
语句进行用户错误处理,我认为这会完成您想要的工作
Private Sub CommandButton_OK_Click()
If nameText.Value = "" Then GoTo InvalidName
'' Do some stuff
On Error GoTo InvalidTime
Let TimeText.Value = TimeValue(TimeText.Value)
On Error GoTo 0
'' Do some more stuff
Let Cells(emptyrow, 9).Value = IIf(CornNo.Value = True, "No", "Yes")
'' Do even more stuff
Exit Sub
InvalidName:
MsgBox "Please Enter a Name", vbInformation + vbOKOnly, "Error"
Exit Sub
InvalidTime:
MsgBox "Please Enter a Valid Time", vbInformation + vbOKOnly, "Error"
Exit Sub
End Sub
我正在尝试在用户表单中进行错误检查,我希望能够检查输入的时间是否是有效的时间表达式(00:00 或 00 PM),因为我正在通过 TimeValue 命令发送它对于输出,以便时间标准化。如果输入的值不是有效表达式,我将显示错误消息并允许重新输入有效表达式。我不确定这是否是我可以使用 Regex 完成的事情,或者是否有更简单的选项。我在下面附上了我的代码。
Private Sub CommandButton_OK_Click() Dim emptyrow As Long 'Error Check If NameText.Value = "" Then MsgBox "Please Enter Valid Name", vbOKOnly Exit Sub End If 'Make Sheet 1 Active Sheet1.Activate 'Determine emptyRow emptyrow = WorksheetFunction.CountA(Range("E:E")) + 1 'Convert Time of Inspection to Time Value TimeText.Value = TimeValue(TimeText.Value) 'Transfer Info Cells(emptyrow, 5).Value = DateText.Value Cells(emptyrow, 6).Value = NameText.Value Cells(emptyrow, 7).Value = ShiftText.Value Cells(emptyrow, 8).Value = TimeText.Value If CornNo.Value = True Then Cells(emptyrow, 9).Value = "No" Else Cells(emptyrow, 9).Value = "Yes" End If If SurgeNo.Value = True Then Cells(emptyrow, 10).Value = "No" Else Cells(emptyrow, 10).Value = "Yes" End If If MillNo.Value = True Then Cells(emptyrow, 11).Value = "No" Else Cells(emptyrow, 11).Value = "Yes" End If If FBedNo.Value = True Then Cells(emptyrow, 12).Value = "No" Else Cells(emptyrow, 12).Value = "Yes" End If If DDGOutNo.Value = True Then Cells(emptyrow, 13).Value = "No" Else Cells(emptyrow, 13).Value = "Yes" End If If DDGInNo.Value = True Then Cells(emptyrow, 14).Value = "No" Else Cells(emptyrow, 14).Value = "Yes" End If Unload Me End Sub
看起来您可能希望使用 On Error GoTo Label
语句进行用户错误处理,我认为这会完成您想要的工作
Private Sub CommandButton_OK_Click()
If nameText.Value = "" Then GoTo InvalidName
'' Do some stuff
On Error GoTo InvalidTime
Let TimeText.Value = TimeValue(TimeText.Value)
On Error GoTo 0
'' Do some more stuff
Let Cells(emptyrow, 9).Value = IIf(CornNo.Value = True, "No", "Yes")
'' Do even more stuff
Exit Sub
InvalidName:
MsgBox "Please Enter a Name", vbInformation + vbOKOnly, "Error"
Exit Sub
InvalidTime:
MsgBox "Please Enter a Valid Time", vbInformation + vbOKOnly, "Error"
Exit Sub
End Sub