我有一个电子表格,可以跟踪各种文件的发送时间。对于每个文件,我都有一个输入框,询问文件发送的时间,并且可以在该输入框中放置一些其他选项。
如果该人没有输入有效的条目,我想将他们发送回输入框,以便他们可以重试。
这是我的代码,适用于第一个错误,但如果有人第二次输入错误的值,他们只会收到标准的“类型不匹配”错误。我正在为那些没有任何 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
最佳答案
我不会使用错误处理程序,而是使用仅在满足条件时才会中断的循环
。例如:
第一次询问:
tm = UCase(Application.InputBox("What time was the CFP file sent?"))
Dim inputOK As Boolean
继续询问,直到用户输入错误的值:
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
其中 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
关于vba - 错误 GoTo 仅工作一次用于分析输入框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29951610/