Home > Software engineering >  How to activate a UserForm OptionButton (Not Assigning it a Value) in VBA
How to activate a UserForm OptionButton (Not Assigning it a Value) in VBA

Time:11-03

I have a userform with some frames which these frames also have some option buttons in them and a commandbutton which I want every time I clicking it, it runs another bit of code based on values of OptionButtons in Frames.

I already know that the e.g. (OptionButton1.Value = True) method will at least visually select OptionButton1 in the userform, but the problem is that it won't actually activate it.

Every time I run this bit of code it gives me "Object Variable Or With Block Variable Not Set" error:

Private Sub CommandButton1_Click()

Select Case Frame3.ActiveControl.Name 'This line drops an error
    case "Name 1"
        'Code Path 1
    case "Name 2"
        'Code Path 2    

end sub

I should mention when I Manually Select an OptionButton before clicking my CommandButton this code runs without error, so this made me an idea that the problem should relate to "Activation" of OptionButtons not their value.

CodePudding user response:

For a control to be active it must have focus. So somewhere in your code, add this line:

OptionButton1.SetFocus

You can also assign a value if needed but this is not required.

  • Related