Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if a Form Control option button is selected in VBA

I have a code that work just fine using ActiveX option buttons. However, I want the macro to run on a Mac as well so I am trying to replace my ActiveX controls with form controls. With ActiveX, all I had to do in order to check if one of my two option buttons was selected is:

    Sub OptionButton1_Click
    If OptionButton1.Value = true then
        (action...)
    End if
    End sub

I have been trying to find an equivalent for Form Controls on Google but each time I get an:

Object required error

Thank you very much for your answers @L42 and @Sai Nishank! Now what if I want to check in an OptionButton_Click if an option button from an other group is true ? I tried this syntax but I get an error message : "Compile error Method or Data not found"

    Sub USDButton_Click()
    MsgBox "USD"
    If Sheet1.BTUButton = True Then
    (action1)
    End If
     If Sheet1.kWhButton = True Then
    (action2)
     End If

I am not sure if BTUButton is the correct name of the button, but I don't where to check, form controls don't have that handy "Right Click > Properties" like ActiveX

like image 370
Ettelaiv Avatar asked Jun 05 '15 03:06

Ettelaiv


People also ask

How do you reference a form control in VBA?

Referring to Controls on Subforms To refer to it, use the name of the control. refers to the subfrmOrders subform on the frmCustomer form as a control. To refer to a control on a subform, use the Form property to tell Access that you are referring to the subform as a form, not as a control.

How do I disable a form control button in Excel VBA?

Without Using VBA CodeClick the desired button > look to the Properties Window (F4) > change the Enabled property to False to disable the button and True to enable the button.


1 Answers

If you are using a Form Control, you can get the same property as ActiveX by using OLEFormat.Object property of the Shape Object. Better yet assign it in a variable declared as OptionButton to get the Intellisense kick in.

Dim opt As OptionButton

With Sheets("Sheet1") ' Try to be always explicit
    Set opt = .Shapes("Option Button 1").OLEFormat.Object ' Form Control
    Debug.Pring opt.Value ' returns 1 (true) or -4146 (false)
End With

But then again, you really don't need to know the value.
If you use Form Control, you associate a Macro or sub routine with it which is executed when it is selected. So you just need to set up a sub routine that identifies which button is clicked and then execute a corresponding action for it.

For example you have 2 Form Control Option Buttons.

Sub CheckOptions()
    Select Case Application.Caller
    Case "Option Button 1"
    ' Action for option button 1
    Case "Option Button 2"
    ' Action for option button 2
    End Select
End Sub

In above code, you have only one sub routine assigned to both option buttons.
Then you test which called the sub routine by checking Application.Caller.
This way, no need to check whether the option button value is true or false.

like image 155
L42 Avatar answered Nov 15 '22 04:11

L42