Consulting

Results 1 to 9 of 9

Thread: MsgBox vbYesNo help

  1. #1

    MsgBox vbYesNo help

    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

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Try something like this

    [VBA]

    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

    [/VBA]

    Paul

  3. #3
    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,

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Like this

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

    Paul

  5. #5
    Thanks again Paul, that's what I needed.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    A textbox will ALWAYS be blank when a form initializes!
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    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.

  8. #8
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    For a little more customization, just set it as its own variable. Here's an example...

    [vba]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[/vba]
    For even more customization (i.e. button names/shapes/sizes).

    HTH

  9. #9
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •