Darren Ignatius Lee
Darren Ignatius Lee

Reputation: 21

DoWhileLoop not working while error-trapping

So basically, what I'm trying to do is to create a simple square-root subroutine, that would take a value via an inputbox, and output the square-root of that value if there is.

If an error occurs in the input process (eg if I input a letter "a"), it would trigger the error handler and asks the user to re-enter a value until the "type mismatch" error disappears (that is when Err.Number=0)

The issue I am getting is that while the error redirects to my error-handler, it only works once. When I re-enter the value as "a" again, it triggers the "type mismatch" error, and the entire sub-routine stops, rather than iteratively prompting the user for a numeric value again as I intended it to be.

Public Sub realnumber()
On Error GoTo line
    Dim rn As Double, re As Double
    rn = InputBox("please enter a real number")
    sqrn = Format(Sqr(rn), "0.00")
    Debug.Print (sqrn)
    Exit Sub

line:
    Select Case Err.Number
        Case 13
            Do While Err.Number = 13
                re = InputBox("re-enter")
            Loop
            rn = re
            Resume
    End Select
End Sub

Upvotes: 0

Views: 39

Answers (1)

SJR
SJR

Reputation: 23081

Why not use the Application.InputBox method and control the type of entry you want (type 1 is a number)?

Public Sub realnumber()

Dim rn As Double, re As Double

rn = Application.InputBox("please enter a real number", Type:=1)
sqrn = Format(Sqr(rn), "0.00")
Debug.Print (sqrn)

End Sub

Upvotes: 3

Related Questions