Input box VBA code

ftsang

New Member
Joined
Jun 9, 2005
Messages
25
Hi Guys,
I am looking for some VBA code which brings up an input box for the user to fill in, which asks a simple yes/no question, such as "Do you own a car?"

Then, I want it to give an error message if the input is not "Yes" or "No".

And ideas?

Thanks!
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Best to not allow any alternative. Try this :-
Code:
Sub test()
    rsp = MsgBox("Do you own a car ", vbYesNo)
    If rsp = vbYes Then
        answer = " Yes"
    Else
        answer = "No"
    End If
End Sub
 
Upvote 0
If you're just going to use a yes/no question, a simple msgbox would be easier. Then they would only have yes/no options--no chance to enter anything else.

Msgbox "Do you own a car?",vbyesno
 
Upvote 0
Code:
Sub Question()
Dim Response As String, AnsweredCorrectly As Boolean

AnsweredCorrectly = False

Do Until AnsweredCorrectly = True
        Response = InputBox("Do you own a car?")
        Select Case Response
        Case "Yes"
            MsgBox "you do own a car"
            AnsweredCorrectly = True
        Case "No"
            MsgBox "You don't own a car"
            AnsweredCorrectly = True
        Case Else
            MsgBox "You didn't respond correctly"
            
        End Select
Loop

ENd sub

HTH
Cal
 
Upvote 0
You can use the MsgBox function like this:

Code:
Sub Test()
    Dim Ans As Variant
    Ans = MsgBox("Do you own a car?", vbQuestion + vbYesNo, "Car Owner")
    If Ans = vbYes Then
'       code for car owner
    Else
'       code for non car owner
    End If
End Sub
 
Upvote 0
VBA input Box Code

Unfortunately I am still stuck.
The method mentioned by Cbrine works the best, but it doesn't let me drop out of the loop if the cancel button is pressed. Help!
 
Upvote 0
Code:
Sub Question()
Dim Response As String, AnsweredCorrectly As Boolean
AnsweredCorrectly = False

Do Until AnsweredCorrectly = True
        Response = InputBox("Do you own a car?")
        Select Case Response
        Case ""
            Exit Do
        Case "Yes"
            MsgBox "you do own a car"
            AnsweredCorrectly = True
        Case "No"
            MsgBox "You don't own a car"
            AnsweredCorrectly = True
        Case Else
            MsgBox "You didn't respond correctly"
            
        End Select
Loop

End Sub

HTH
Cal
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,730
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top