Hello learning_vba,
Welcome to the Forum!
As a general rule, we do not provide answers to homework questions. The reason is obvious. In this case I will make an exception because your assignment doesn't have anything to do with looping structures but with validation and conditional transfer of execution.
This macro will transfer control back to the line label "GetInput" based a series of validation tests. If any validation step fails, execution returns back to this line to ask the user to input a value again. An error dialog will be displayed alerting the user to the mistake.
Sub InputTest()
Dim Answer As Variant
GetInput:
Answer = InputBox("Enter a number from 1 to 5.")
'Did user click Cancel?
If Answer = "" Then
Exit Sub
End If
'Is input a number?
If Not IsNumeric(Answer) Then
MsgBox "Please enter a number from 1 to 5."
GoTo GetInput
End If
'Convert Answer from a string to an Integer
Answer = CInt(Answer)
'Is number 1 to 5?
If Answer < 1 Or Answer > 5 Then
MsgBox "Please enter a number from 1 to 5."
GoTo GetInput
End If
End Sub
Bookmarks