togglebutton to act like optionbutton

R

ranswrt

I have a worksheet with 5 toggle buttons. I need to get them to act like
option buttons where when one is clicked the other values will be set to
false. I tried using the click event for each togglebutton to set the other
toggles values to false, but as each one is set to false, the click event for
that toggle runs. Any ideas how I can get this to work or is there a way to
make option buttons look like toggle buttons?
Thanks
 
O

OssieMac

Unfortunately it does not appear that you can suppress events from running
with the toggle buttons using Application.EnableEvents. However, I
successfully got the following to work. I set a linked cell for each of the
Toggle buttons so that I could observe their behaviour and it appears to work
OK.

Dim bolToggle As Boolean

Private Sub ToggleButton1_Click()
MsgBox "ToggleButton1_Click ran" & ToggleButton1.Value
If ToggleButton1 = True Then
bolToggle = False
ToggleButton2.Value = False
ToggleButton3.Value = False
ToggleButton4.Value = False
ToggleButton5.Value = False
End If

bolToggle = True

End Sub

Private Sub ToggleButton2_Click()
If bolToggle = True Then
MsgBox "ToggleButton2_Click ran" & ToggleButton2.Value
End If
End Sub

Private Sub ToggleButton3_Click()
If bolToggle = True Then
MsgBox "ToggleButton3_Click ran" & ToggleButton3.Value
End If
End Sub

Private Sub ToggleButton4_Click()
If bolToggle = True Then
MsgBox "ToggleButton4_Click ran" & ToggleButton4.Value
End If
End Sub

Private Sub ToggleButton5_Click()
If bolToggle = True Then
MsgBox "ToggleButton5_Click ran" & ToggleButton5.Value
End If
End Sub
 
O

OssieMac

Had another look at this and if my assumption is right that you only want
your code to run when the option button is true then I think that this is a
simpler option. It changes all other options to false and you can put your
code in after the code that changes the other option buttons:-

Private Sub ToggleButton1_Click()
If ToggleButton1 = True Then
ToggleButton2.Value = False
ToggleButton3.Value = False
ToggleButton4.Value = False
ToggleButton5.Value = False
'Any other code here

End If

End Sub

Private Sub ToggleButton2_Click()
If ToggleButton2 = True Then
ToggleButton1.Value = False
ToggleButton3.Value = False
ToggleButton4.Value = False
ToggleButton5.Value = False
'Any other code here

End If

End Sub

Private Sub ToggleButton3_Click()
If ToggleButton3 = True Then
ToggleButton1.Value = False
ToggleButton2.Value = False
ToggleButton4.Value = False
ToggleButton5.Value = False
'Any other code here
End If

End Sub

Private Sub ToggleButton4_Click()
If ToggleButton4 = True Then
ToggleButton1.Value = False
ToggleButton2.Value = False
ToggleButton3.Value = False
ToggleButton5.Value = False
'Any other code here

End If

End Sub

Private Sub ToggleButton5_Click()
If ToggleButton5 = True Then
ToggleButton1.Value = False
ToggleButton2.Value = False
ToggleButton3.Value = False
ToggleButton4.Value = False
'Any other code here


End If
End Sub
 
R

ranswrt

I think that will work.
Thanks

OssieMac said:
Had another look at this and if my assumption is right that you only want
your code to run when the option button is true then I think that this is a
simpler option. It changes all other options to false and you can put your
code in after the code that changes the other option buttons:-

Private Sub ToggleButton1_Click()
If ToggleButton1 = True Then
ToggleButton2.Value = False
ToggleButton3.Value = False
ToggleButton4.Value = False
ToggleButton5.Value = False
'Any other code here

End If

End Sub

Private Sub ToggleButton2_Click()
If ToggleButton2 = True Then
ToggleButton1.Value = False
ToggleButton3.Value = False
ToggleButton4.Value = False
ToggleButton5.Value = False
'Any other code here

End If

End Sub

Private Sub ToggleButton3_Click()
If ToggleButton3 = True Then
ToggleButton1.Value = False
ToggleButton2.Value = False
ToggleButton4.Value = False
ToggleButton5.Value = False
'Any other code here
End If

End Sub

Private Sub ToggleButton4_Click()
If ToggleButton4 = True Then
ToggleButton1.Value = False
ToggleButton2.Value = False
ToggleButton3.Value = False
ToggleButton5.Value = False
'Any other code here

End If

End Sub

Private Sub ToggleButton5_Click()
If ToggleButton5 = True Then
ToggleButton1.Value = False
ToggleButton2.Value = False
ToggleButton3.Value = False
ToggleButton4.Value = False
'Any other code here


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

Top