Simple vbYesNo... lol...

albertc30

Well-known Member
Joined
May 7, 2012
Messages
1,091
Office Version
  1. 2019
Platform
  1. Windows
Hi all.

I hope everyone is well and keeping safe.

Stupid question I know, but for the life of me why is this not working? I'm rubbish at this.

My 8 year old, 9 this Saturday, has taken a keen interest on coding, bless. As such he's starting with excel as he's seen me doing a few things.

He's just got a msgbox and is playing around the yes and no side of it.

Now, my code was the following as just an example for him;

VBA Code:
Sub now02()
MsgBox "are you sure?", vbYesNo
If vbYes Then
    MsgBox ("You clicked Yes!")    
End If
Exit Sub
If vbNo Then
    MsgBox ("You clicked No!")
Exit Sub
End If
End Sub

I'm getting the same answer (You clicked Yes!) regardless of whether i click yes or no.

What on earth am I doing wrong here? I'll never be good at this.

Soon enough he'll be teaching me.

As always much appreciated for all the help.

Albert
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Two things:

First, if you want to check the value of what they selected, you should set the MsgBox equal to a variable.
Second, your first "Exit Sub" is outside of your first IF...THEN block, so you will always hit that and never get to the second IF...THEN block.

Try this:
VBA Code:
Sub now02()

Dim msg

msg = MsgBox("are you sure?", vbYesNo)

If msg = vbYes Then
    MsgBox ("You clicked Yes!")
    Exit Sub
End If

If msg = vbNo Then
    MsgBox ("You clicked No!")
    Exit Sub
End If

End Sub
 
Upvote 0
You could also do it like
VBA Code:
Sub Albert()
   If MsgBox("Are you sure?", vbYesNo) = vbYes Then
      MsgBox "you clicked yes"
   Else
      MsgBox "you clicked No"
   End If
End Sub
 
Upvote 0
Too advanced a construction for someone new to programming, but just letting you know it is a possibility for the future...
VBA Code:
Sub Albert2()
  MsgBox "You clicked " & Choose(MsgBox("Are you sure?", vbYesNo) - 5, "yes", "no")
End Sub
or a smidge shorter...
VBA Code:
Sub Albert3()
  MsgBox "You clicked " & Split("yes no")(MsgBox("Are you sure?", vbYesNo) - 6)
End Sub
 
Upvote 0
Oh you guys.

Joe4, Fluff and Rick my man, you're still around. Thanks ever so much. This forum has helped me over many many years now and never has disapointed me.

To you all and everybody here, giving their time and knowledge to help other many many thanks.

Now my kid is, like me once and still am at times, amazed with the endless potential ways of doing one thing in vb.

You guys keep up the good work and be safe.

Albert.
 
Upvote 0

Forum statistics

Threads
1,215,660
Messages
6,126,089
Members
449,288
Latest member
DjentChicken

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