Vba 错误发生后循环回输入框
在下面做一个快速练习,一个可以将英寸转换为厘米的输入框。 我想添加到错误处理程序部分,以便在出现msgbox解释发生错误后,它会返回到inputbox,要求我添加英寸以转换为厘米。谢谢Vba 错误发生后循环回输入框,vba,Vba,在下面做一个快速练习,一个可以将英寸转换为厘米的输入框。 我想添加到错误处理程序部分,以便在出现msgbox解释发生错误后,它会返回到inputbox,要求我添加英寸以转换为厘米。谢谢 Sub Exercise() Dim m As Variant Dim result Dim ErrM As String On Error GoTo ErrHandler m = InputBox("How much in Inches would you like to convert to
Sub Exercise()
Dim m As Variant
Dim result
Dim ErrM As String
On Error GoTo ErrHandler
m = InputBox("How much in Inches would you like to convert to Centimeters?", "Inches to Centimeters", "Please Type here")
result = m * 2.54
MsgBox "There are " & result & " inches", , "Result"
Exit Sub
ErrHandler:
MsgBox ("An error occured, please type a number into the input box")
End Sub
请这边走:
Sub Exercise()
Dim m As Variant, result As Double
BackInp:
m = InputBox("How much in Inches would you like to convert to Centimeters?", "Inches to Centimeters", "Please Type here")
If Not IsNumeric(m) Then
MsgBox "An error occurred, please type a numeric value into the input box!", _
vbInformation, "Need to repeate the input..."
GoTo BackInp
End If
result = m * 2.54
MsgBox "There are " & result & " inches", , "Result"
End Sub
在您的特定情况下,不需要进行错误处理。简单地说,将InputBox放入一个循环中,检查输入是否是一个数字(例如,使用
IsNumeric
),然后根据该数字决定是继续还是重复
例如:
Do While True
m = InputBox("How much in Inches would you like to convert to Centimeters?", _
"Inches to Centimeters", "Please Type here")
If StrPtr(m) = 0 Then
' The user canceled the operation.
Exit Sub
ElseIf Not IsNumeric(m) Then
' The input is not a number.
MsgBox ("Please type a number into the input box.")
Else
' The input is a number. We continue with the code below.
Exit Do
End If
Loop
result = m * 2.54
MsgBox "There are " & result & " inches", , "Result"
谢谢你,我的朋友