PDA

View Full Version : MsgBox vbYesNo help



Killer Bud
05-13-2010, 03:39 PM
Now for my next problem. I have created the following msgbox:


Public Sub OkButton_Click()
MsgBox "Would you like to price another gasket?", vbyesNo


......how do I define whether or not yes or no are selected?
I ultimately would like "yes" to reload userform1 again and "no" to close the workbook ( or excel altogether ). I am frustrated now as this should be easy.

This is what I have tried....please let me know what I'm doing wrong so I can learn ( I am a complete newbie on day 2 of VBA )

If ("MsgBox = vbyes") Then userform1.show
If ("MsgBox = vbno") Then activeworkbook.close savechanges:=true

Thanks for any help you can give me

Paul_Hossler
05-13-2010, 03:55 PM
Try something like this



Sub drv()
If MsgBox("Well?", vbYesNo, "User Input") = vbYes Then
Call MsgBox("You said Yes", vbOKOnly, "User Anwser")
Else
Call MsgBox("You said No", vbOKOnly, "User Anwser")
End If

End Sub



Paul

Killer Bud
05-13-2010, 05:18 PM
That gave me the basis of what I want to do, thank you.
Nice to finally get this to work.

One other thing.....If I have text boxes in my userform that send their data to a cell on sheet 1 ( for example, A20), how can I automatically have this cell & text box ( among others ) automatically clear itself upon startup of the userform ?

Thanks,

Paul_Hossler
05-13-2010, 06:35 PM
Like this


Private Sub UserForm_Initialize()
TextBox1.Text = ""
Worksheets("Sheet1").Range("A20").ClearContents
End Sub


Paul

Killer Bud
05-14-2010, 08:05 AM
Thanks again Paul, that's what I needed.

Bob Phillips
05-14-2010, 08:14 AM
A textbox will ALWAYS be blank when a form initializes!

Killer Bud
05-14-2010, 09:13 AM
The textbox was blank, but the cell still had data. This just stops the user from pressing ok and getting a cost without entering anything into the textbox.

Zack Barresse
05-14-2010, 01:00 PM
For a little more customization, just set it as its own variable. Here's an example...

Sub TrapMsgBox()

'Dimension variables
Dim sPrompt As String
Dim msgTrap As VbMsgBoxResult

'Set string variable
sPrompt = "Please click a button. Click on! NOW!"

'Display/get message box and set to variable
msgTrap = MsgBox(sPrompt, vbYesNoCancel, "MY TITLE HERE")

'Check/trap for result of user click
Select Case msgTrap
Case vbYes
'Yes was clicked
MsgBox "Yes was clicked", vbInformation, "YES"
Case vbNo
'No was clicked
MsgBox "No was clicked", vbExclamation, "NO"
Case vbCancel
'Cancel was clicked
MsgBox "Cancel was clicked", vbCritical, "CANCEL"
End Select

End Sub
For even more customization (i.e. button names/shapes/sizes).

HTH

Killer Bud
05-14-2010, 01:16 PM
Thanks Zack. I am in the very early learning stages of vba, but everybody on this site has been extremely helpful and I have learned alot in only 3 days. Thanks again.