Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use VBA to clear cells A1 to A10 b. Use an input box to ask the user to enter a

ID: 3819184 • Letter: U

Question

Use VBA to clear cells A1 to A10 b. Use an input box to ask the user to enter a string without any blanks (leading, trailing or in between) and assign it to the string variable str W. c. If the string str W entered is an empty string, then use a message box to tell the user something like "The string entered is empty!" d. If the string str W entered has any blanks, then use a message box to tell the user something like "The string entered must contain no blanks!". e. If the string str W entered is an empty string or str W has any blanks, repeat step b until a nonempty string without blanks is entered. f. If a correct string str W is entered, then use a message box with Yes and No buttons to ask the user "Do you want to create a password with str W?" g. If the user responds No then no password is generated. Interrupt execution of the code so that no other statements are executed. h. If the user responds Yes then create a password as the string str W reversed + length of str W. For example when "gigem" is entered, the password would be "megig5". i. Write the password to the cell "A#" where # is the length of str W. For example when "gigem" is entered, # is 5.

Explanation / Answer

Hi, Please create a command button in your excel sheet and write the below code in the

command button click event:

Private Sub CommandButton1_Click()
Dim strW As String
Dim flag As Boolean
Dim qs As String
Dim length As Integer
Dim i As Integer
Dim rev As String

flag = True
Range("A1:A10").Clear
Do
strW = InputBox("Please enter a string without any blanks")
If StrPtr(strW) = 0 Then
Exit Sub
  
ElseIf Trim(strW) = "" Then
MsgBox ("The Sring entered is empty.")
flag = False
  
  
ElseIf InStr(strW, " ") > 0 Then
MsgBox ("The String entered must contain no blanks")
flag = False
  
  
ElseIf MsgBox("Do you want to create a password with strW?", vbYesNo) = vbYes Then
length = Len(strW)
rev = StrReverse(strW)
rev = rev & length
Range("A" & length).Value = rev
Else
Exit Sub
End If
Loop While (flag = False)

End Sub

Output : Got the result as mentioned in the instructions. Hope this helps you. Thank you.