Consulting

Results 1 to 5 of 5

Thread: Solved: How to code a Yes/No MsgBox under a command button!

  1. #1
    VBAX Regular
    Joined
    Sep 2009
    Posts
    36
    Location

    Question Solved: How to code a Yes/No MsgBox under a command button!

    Hey I am new to these forums. Can I ask, how do I code the following:[vba]Sub Msgbox_Yes_No()
    Dim Response As Integer

    ' Displays a message box with the yes and no options.
    Response = MsgBox(prompt:="Are you sure you want to continue with your choice? You can't go back after your choice.", Buttons:=vbYesNo)

    ' If statement to check if the yes button was selected.
    If Response = vbYes Then
    MsgBox "Proceed to build your laptop."
    ActivePresentation.SlideShowWindow.View.Next
    Else
    ' The no button was selected.
    MsgBox "Please pick the other option"
    End If
    End Sub
    [/vba] under a command button? I tried to copy and paste the following code into the command button but came up with an error.

    This is what I have been typing in:
    [vba]Private Sub CommandButton1_Click()
    Sub Msgbox_Yes_No()
    Dim Response As Integer

    ' Displays a message box with the yes and no options.
    Response = MsgBox(prompt:="Are you sure you want to continue with your choice? You can't go back after your choice.", Buttons:=vbYesNo)

    ' If statement to check if the yes button was selected.
    If Response = vbYes Then
    MsgBox "Proceed to build your laptop."
    ActivePresentation.SlideShowWindow.View.Next
    Else
    ' The no button was selected.
    MsgBox "Please pick the other option"
    End If
    End Sub
    End Sub
    [/vba] What do I need to fix? Thanks.

  2. #2
    VBAX Contributor
    Joined
    May 2008
    Posts
    198
    Location
    Either:
    [vba]Private Sub CommandButton1_Click()
    Dim Response As Integer

    ' Displays a message box with the yes and no options.
    Response = MsgBox(prompt:="Are you sure you want to continue with your choice? You can't go back after your choice.", Buttons:=vbYesNo)

    ' If statement to check if the yes button was selected.
    If Response = vbYes Then
    MsgBox "Proceed to build your laptop."
    ActivePresentation.SlideShowWindow.View.Next
    Else
    ' The no button was selected.
    MsgBox "Please pick the other option"
    End If
    End Sub
    [/vba]
    or:
    [vba]Private Sub CommandButton1_Click()
    Msgbox_Yes_No
    End Sub
    Sub Msgbox_Yes_No()
    Dim Response As Integer

    ' Displays a message box with the yes and no options.
    Response = MsgBox(prompt:="Are you sure you want to continue with your choice? You can't go back after your choice.", Buttons:=vbYesNo)

    ' If statement to check if the yes button was selected.
    If Response = vbYes Then
    MsgBox "Proceed to build your laptop."
    ActivePresentation.SlideShowWindow.View.Next
    Else
    ' The no button was selected.
    MsgBox "Please pick the other option"
    End If
    End Sub
    [/vba]
    if you have multiple buttons you want to run the same Msgbox_Yes_No code.

  3. #3
    VBAX Regular
    Joined
    Sep 2009
    Posts
    36
    Location
    Quote Originally Posted by Cosmo
    Either:
    [vba]Private Sub CommandButton1_Click()
    Dim Response As Integer

    ' Displays a message box with the yes and no options.
    Response = MsgBox(prompt:="Are you sure you want to continue with your choice? You can't go back after your choice.", Buttons:=vbYesNo)

    ' If statement to check if the yes button was selected.
    If Response = vbYes Then
    MsgBox "Proceed to build your laptop."
    ActivePresentation.SlideShowWindow.View.Next
    Else
    ' The no button was selected.
    MsgBox "Please pick the other option"
    End If
    End Sub
    [/vba] or:
    [vba]Private Sub CommandButton1_Click()
    Msgbox_Yes_No
    End Sub
    Sub Msgbox_Yes_No()
    Dim Response As Integer

    ' Displays a message box with the yes and no options.
    Response = MsgBox(prompt:="Are you sure you want to continue with your choice? You can't go back after your choice.", Buttons:=vbYesNo)

    ' If statement to check if the yes button was selected.
    If Response = vbYes Then
    MsgBox "Proceed to build your laptop."
    ActivePresentation.SlideShowWindow.View.Next
    Else
    ' The no button was selected.
    MsgBox "Please pick the other option"
    End If
    End Sub
    [/vba] if you have multiple buttons you want to run the same Msgbox_Yes_No code.
    Top guy, works a treat.

  4. #4
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    It does but you can streamline it to

    [VBA]If MsgBox(prompt:="Are you sure you want to continue with your choice? You can't go back after your choice.", Buttons:=vbYesNo) = vbYes Then
    'Go on
    Else: 'Quit
    End If[/VBA]
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  5. #5
    VBAX Regular
    Joined
    Sep 2009
    Posts
    36
    Location
    Thanks John, do you think you can answer this query for me too? I don't have enough posts to insert a link, so it is called something along the lines of 4 options in a dropdown menu.

    Much appreciated.

Posting Permissions

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