Ken*_*dra 2 excel vba excel-vba onerror
我有一个电子表格,可以跟踪各种文件的发送时间.对于每个文件,我有一个输入框,询问文件发送的时间,还有一些其他选项可以放在该输入框中.
如果此人没有输入有效的条目,我想将它们发送回输入框,以便他们可以再试一次.
这是我的代码,适用于第一个错误,但如果有人第二次输入错误的值,他们只会得到标准的"类型不匹配"错误.我正在为那些对VBA没有任何了解的人创建这个,并希望尽可能简单.
以下代码有多个实例,每个实例用于不同的文件时间.提前致谢!
CFP:
If Err Then MsgBox Error & " Invalid value. Please enter only 'blank', 'holiday', 'skip' or time value."
Err.Clear
tm = UCase(Application.InputBox("What time was the CFP file sent?"))
On Error GoTo CFP
If tm = "BLANK" Then
ActiveCell.Value = "Blank"
ElseIf tm = "HOLIDAY" Then
ActiveCell.Value = "Holiday"
ElseIf tm = "SKIP" Then
ActiveCell.Value = "Skipped"
Else
ActiveCell.Value = TimeValue(tm)
End If
Run Code Online (Sandbox Code Playgroud)
而不是使用错误处理程序,我会使用Loop只有在满足条件时才会中断的错误处理程序.例如:
第一次询问:
tm = UCase(Application.InputBox("What time was the CFP file sent?"))
Dim inputOK As Boolean
Run Code Online (Sandbox Code Playgroud)
继续询问,直到用户输入错误的值:
inputOK = checkMyInput(tm) '<-- is the input "tm" ok or not?
Do While inputOK = False '<-- if it's not ok, keep on asking
MsgBox "Please enter a valid input"
tm = UCase(Application.InputBox("What time was the CFP file sent?"))
inputOK = checkMyInput(tm) '<-- and checking it again...
Loop
Run Code Online (Sandbox Code Playgroud)
checkMyInput(tm)返回的自定义函数在哪里,True或者False输入是否正常:
Function checkMyInput(ByVal tm As Variant) As Boolean
If tm ="BLANK" Or tm = "HOLIDAY" Or tm = "SKIP" Or IsDate(tm) Then
checkMyInput = True
Else
checkMyInput = False
End If
End Function
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
413 次 |
| 最近记录: |