Consulting

Results 1 to 7 of 7

Thread: Solved: MsgBox problem

  1. #1
    VBAX Regular
    Joined
    Nov 2005
    Location
    Jefferson City
    Posts
    13
    Location

    Solved: MsgBox problem

    When I use this code I get the response for a yes answer even when no is clicked. What is wrong?

    [VBA]
    MsgBox "Do you want to find someone else? ", vbYesNo, ""
    If vbYes Then
    UserForm1.Show
    Else
    Exit Sub
    End If

    [/VBA]

    If I click no I still get the userform to show. This happens to me all the time and I get frustrated and use a second userform just to get the result I want.

    Thanks for any advice, help, or beer!
    I'm here to help! cmyers1032@aol.com

  2. #2
    Administrator
    2nd VP-Knowledge Base
    VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Hi Viper Welcome to the forums!

    I can't explain why that is happening the way it is...and sorry I can't send ya any beer ...but try this:
    [VBA]
    Sub Userform()
    If MsgBox("Do you want to find someone else? ", vbYesNo, "") = vbYes Then
    UserForm1.Show
    Else
    Exit Sub
    End If
    End Sub
    [/VBA]
    HTH




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  3. #3
    VBAX Regular vonpookie's Avatar
    Joined
    Jun 2004
    Location
    Are we there yet?
    Posts
    74
    Location
    Basically, you are selecting a response, but it is not connected to the values in the If statement at all. If that makes any sense (which it probably doesn't).

    Malik's got the right idea, but I usually just use a variable, myself:

    [vba]Dim ans As Variant

    ans = MsgBox("Do you want to find someone else? ", vbYesNo)
    If ans = vbYes Then
    UserForm1.Show
    Else
    Exit Sub
    End If[/vba]

  4. #4
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Joesph's code is the way to go.
    The reason being that MsgBox is a function that returns a value (depending on which button is clicked)
    So you test the value the function throws back in the same line (in this case vbYes which is a xl constant - value: 6)... then do something

    Hope that explains it
    K :-)

  5. #5
    VBAX Regular
    Joined
    Nov 2005
    Location
    Jefferson City
    Posts
    13
    Location
    Thanks each of you. I understand now. Never stop learning, everyday is a learning experience at some level.
    I'm here to help! cmyers1032@aol.com

  6. #6
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    Quote Originally Posted by Killian
    Joesph's code is the way to go.
    The reason being that MsgBox is a function that returns a value (depending on which button is clicked)
    So you test the value the function throws back in the same line (in this case vbYes which is a xl constant - value: 6)... then do something

    Hope that explains it
    Hello Viper! Welcome to VBAX!

    Support Joseph idea here.

  7. #7
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Quote Originally Posted by Killian
    Joesph's code is the way to go.
    Quote Originally Posted by Sheeng
    Support Joseph idea here.
    Thanks for the support guys!

    Quote Originally Posted by viper
    Thanks each of you. I understand now. Never stop learning, everyday is a learning experience at some level.
    Glad to help! And no, never stop learning




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

Posting Permissions

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