Very new to coding with VBA in excel, what I'm trying to do is have a Do While loop that takes user input and if it is either not a number or less than 0 returns an error message and re-prompts the user to enter a value until it hits the prerequisites. I have tried doing this in a few ways but cant seem to figure out the specifics behind it. My code currently looks like:
Function getQuantity(prompt)
Dim quantity As String
quantity = InputBox(prompt)
Do While quantity < 0
If Not IsNumeric(quantity) Then
MsgBox ("Incorrect Value, Numbers Only Please!")
quantity = InputBox(prompt)
End If
Loop
End Function
When written like this I get runtime errors.
If I just use:
Function getQuantity(prompt)
Dim quantity As String
quantity = InputBox(prompt)
If Not IsNumeric(quantity) Then
MsgBox ("Incorrect Value, Numbers Only Please!")
quantity = InputBox(prompt)
End If
End Function
this, it does what I want except it only re-prompts once before continuing because it is not in a loop. Basically I want it to loop until it gets a number that is 0 or greater, and displays the error message and re-prompts the user until this happens.
Thanks in advance for any help!
Do While quantity < 0before checking if it's a number is probably not going to work.