state of option button

  • Thread starter Thread starter crapit
  • Start date Start date
C

crapit

I have 2 option button. How do I get the state of the option button? (The
optionbutton is not on the userform)
 
If the button is from the Forms toolbar, use code like

Debug.Print ActiveSheet.OptionButtons(1).Value

If the button is from the Controls toolbar, use code like

Debug.Print ActiveSheet.OLEObjects("OptionButton1").Object.Value


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
I'm using forms toolbar. Does the "optionsbutton(1)" refer to the name of
the optionbutton or something else?
 
The "optionbuttons(1)" refers to the first (earliest created)
option button on the sheet. Alternatively, you can use the name
of the option button. E.g.,

Debug.Print ActiveSheet.OptionButtons("TheButton").Value


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Do I put the code within the sub function itself? E.g the groupname of the
2 optionbutton is "Group19"
Sub group19_click()
<Should the code be put here?>
End sub
 
Do something like

Dim V as Long
V = ActiveSheet.OptionButtons(1).Value
If V = 1 Then
' button is true
Else
' button is false
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Your best bet is to give specific names to your option buttons
rather than rely on index numbers.
 
I get this error "Unable to get OptionButtons property of the worksheet
class"
 
You'll get this error if you attempt to use a name that doesn't
exist. Be sure you are using the correct name of the option
button.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
I still got the error!
The following are the codes:
Sub Group1119_Click()
x = ActiveSheet.OptionButtons("optionbutton1").Value

If x = 1 Then
Range("e1").Select
ActiveCell.Value = "Remove"
Else
Range("e1").Select
ActiveCell.Value = "Insert"
End If

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top