In: Computer Science
Write a program for a beauty store that uses an InputBox to ask the guest for a membership code. Embed this in a Do loop so that the user has to keep trying until the result is a valid membership code that starts with “Beauty” (case insensitive) and is followed by 4 digits with the final digit equal to either 6 or 8. Then use a message box to display the input promotion code and inform the user that the code has been applied. Hint: You can check if a character is a digit by seeing if it is >= “0” and if it is <= “9” (the quotation marks are needed).
Insert this code in any excel VBA editor module (ALT +F11 >> Module > Insert)
Sub LoopInputBoxCheck()
'Scope: An example how to keep looping an input box until unless a valid answer is entered
'
Dim InputQues As String
Dim Answer As Variant
Dim X As Boolean
X = False
'Question to pose to the user
InputQues = "Please enter Mebmership code" & vbNewLine _
& "(Hint: It starts with Beauty)"
'Keep looping until you get a valid answer to your question
Do
'Retrieve an answer from the user
Answer = Application.InputBox(InputQuest, "Your Answer")
If Left(Answer, 6) = "Beauty" Then If Len(Answer) = 10 Then If Right(Answer, 1) = 6 Or Right(myAnswer, 1) = 8 Then X = True
If X = True Then MsgBox ("Code " & Answer & "has been applied")
If X = True Then Exit Sub
Loop While Answer <= 0 Or Answer > 120
End Sub