How do I make sure an OptionButton.Value has been chosen
I currently making an Excel VB application. However, I am stuck at the moment. I am trying to make the choice between two OptionButtons compulsory.
I tried the code
If Me.PWebOption.Value = False & Me.BWebOption = False Then
MsgBox "Please choose a type of website"
Exit Sub
End If
But this is not giving me the required results. I tried putting them in brackets and everything but still stuck. I'm sure this is 开发者_如何学编程a trivial answer but I am just getting to grips with VB so please forgive me.
Many thanks in advance.
I tried your code and it works if you change & to And. I created 2 OptionButtons, named them and ran the code with "And" without problems.
If it still doesn't work then my guess is that:
- one or both of your OptionButtons is not false
or:
- you are accessing the wrong OptionButtons.
Try
Debug.Print Me.PWebOption.Value
Debug.Print Me.BWebOption.Value
Debug.Print Me.PWebOption 'just for fun'
Debug.Print Me.BWebOption
It should all return false. Also try putting Option Explicit
at the head of your Code to ensure you are only accessing objects that actually exist.
Note: In VBA & is only used for concatenation and: And, Or and Not are the logical operators.
I'm not sure if it's a copy/paste error or not, but in the first part you're calling .Value
and in the second part you're just using the option itself. Should your code be this instead:
If Me.PWebOption.Value = False & Me.BWebOption.Value = False Then
MsgBox "Please choose a type of website"
Exit Sub
End If
(Note that it's using .Value
on both options.)
精彩评论