Solved Exercise
Exercise Solution
-
Start by declaring the variables and assigning the MsgBox answer to a variable
Dim Ans As String
Dim Ans2 As String
Dim Name As String
Ans = MsgBox("Is your name " & Application.UserName & "?", vbYesNo, "SuperExcelVBA")
-
Continue by creating a InputBox to change the user name if the answer is no
If Ans = vbNo Then
Name = InputBox("What is your name?", "SuperExcelVBA")
-
Make a conditional If to stop the routine if the input box is left in blank
If Name = vbNullString Then
Exit Sub
End If
-
Now with a new user input, we create a new question with two possible answers
Ans2 = MsgBox("Sorry for my mistake " & Name & "! Will not occur again." _
& vbCrLf & vbCrLf & "Forgive?!", vbYesNo, "SuperExcelVBA")
If Ans2 = vbNo Then
MsgBox "The mistake is really unforgivable! We're sorry.", vbCritical, "SuperExcelVBA"
Else
MsgBox "Thank you very much, sir!"
End If
-
Finally, create a message to be displayed if the answer to the user name question is affirmative
Else
MsgBox "I am a fortune teller! ;-)", , "SuperExcelVBA"
End If
Consolidated Answer
Sub Solution()
Dim Ans As String
Dim Ans2 As String
Dim Name As String
Ans = MsgBox("Is your name " & Application.UserName & "?", vbYesNo, "SuperExcelVBA")
If Ans = vbNo Then
Name = InputBox("What is your name?", "SuperExcelVBA")
If Name = vbNullString Then
Exit Sub
End If
Ans2 = MsgBox("Sorry for my mistake " & Name & "! Will not occur again." & vbCrLf & vbCrLf & "Forgive?!", vbYesNo, "SuperExcelVBA")
If Ans2 = vbNo Then
MsgBox "The mistake is really unforgivable! We're sorry.", vbCritical, "SuperExcelVBA"
Else
MsgBox "Thank you very much, sir!"
End If
Else
MsgBox "I am a fortune teller! ;-)", , "SuperExcelVBA"
End If
End Sub
SuperExcelVBA.com is learning website. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. All Rights Reserved.
Excel ® is a registered trademark of the Microsoft Corporation.
© 2024 SuperExcelVBA | ABOUT