The last time this question was asked it was wrong. An InputBox returns a string – whatever the user enters in the box. However, it returns a blank string if the user enters nothing and clicks on OK or on Cancel. Write a sub that uses an InputBox to ask the user for a product code. Embed this in a Do Loop so that the user has to keep trying until the result is not a blank string.
Expert Answer
Private Sub CommandButton1_Click()
Dim amount As Variant
amount = “”
‘*************Implementation 1**************************
Do
amount = InputBox(“Please enter the Product Code”)
Cells(1, 2).Value = amount
Loop Until amount <> “”
‘*************Implementation 2**************************
Do
amount = InputBox(“Please enter the Product Code”)
Cells(1, 2).Value = amount
Loop While amount <> “”
‘**************Implementation 3*************************
amount = InputBox(“Please enter the Product Code”)
Do While amount <> “”
amount = InputBox(“Please enter the Product Code”)
Cells(1, 2).Value = amount
Loop
End Sub
VBA code-
Private Sub CommandButton1_Click()
Dim amount As Variant
amount = “”
Do
amount = InputBox(“Please enter the Product Code”)
Cells(1, 2).Value = amount
Loop While amount <> “”
End Sub
Screenshot-
Thanks and Regards,
Vinay Prakash Singh