VBA only one toggle button pressed at the time in UserForm

Kra

Board Regular
Joined
Jul 4, 2022
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Hi all!

I have an user form with 5 toggle buttons. Is there any easy and simple code to keep only one of the buttons on value = true?

So if one button is toggled, then others are untoggled.

UserForm name: EventRegist
Toggle Buttons name are all different, but it is not a problem if I have to go back to the default names (ToggleButton1, ToggleButton2 etc)
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Toggle Buttons name are all different
No problem, they can have different names.
In fact you can see it in my examples, just update the code with the names of your togglebuttons

Try this:
VBA Code:
Dim Changing As Boolean             '<--- At the start of all code

Private Sub ToggleButton1_Click()
  Call OnlyOneToggle(ToggleButton1)
End Sub
Private Sub TgB2_Click()
  Call OnlyOneToggle(TgB2)
End Sub
Private Sub Toggle3_Click()
  Call OnlyOneToggle(Toggle3)
End Sub
Private Sub ToggleButton4_Click()
  Call OnlyOneToggle(ToggleButton4)
End Sub
Private Sub ToggleButton5_Click()
  Call OnlyOneToggle(ToggleButton5)
End Sub

Sub OnlyOneToggle(myToggle As MSForms.ToggleButton)
  Dim ctr As Control
  If Changing = True Then Exit Sub
  Changing = True
  For Each ctr In Me.Controls
    If TypeName(ctr) = "ToggleButton" And ctr.Name <> myToggle.Name Then ctr.Value = False
  Next
  Changing = False
End Sub

--------------
I hope to hear from you soon.
Respectfully
Dante Amor
--------------
 
Upvote 1
Solution
OK, this time I'm 5 minutes late. Going to give up for tonight and watch baseball. The name being passed is case sensitive.
VBA Code:
Private Sub ToggleButton1_Click()
ToggleToggles ("ToggleButton1")
End Sub

Private Sub ToggleButton2_Click()
ToggleToggles ("ToggleButton2")
End Sub

Private Sub ToggleButton3_Click()
ToggleToggles ("ToggleButton3")
End Sub

Sub ToggleToggles(ctlName As String)
Dim ctl As control
For Each ctl In Me.Controls
     Debug.Print ctl.Name
     If TypeName(ctl) = "ToggleButton" Then If ctl.Name <> ctlName Then ctl = False
Next

End Sub
 
Upvote 1
No problem, they can have different names.
In fact you can see it in my examples, just update the code with the names of your togglebuttons

Try this:
VBA Code:
Dim Changing As Boolean             '<--- At the start of all code

Private Sub ToggleButton1_Click()
  Call OnlyOneToggle(ToggleButton1)
End Sub
Private Sub TgB2_Click()
  Call OnlyOneToggle(TgB2)
End Sub
Private Sub Toggle3_Click()
  Call OnlyOneToggle(Toggle3)
End Sub
Private Sub ToggleButton4_Click()
  Call OnlyOneToggle(ToggleButton4)
End Sub
Private Sub ToggleButton5_Click()
  Call OnlyOneToggle(ToggleButton5)
End Sub

Sub OnlyOneToggle(myToggle As MSForms.ToggleButton)
  Dim ctr As Control
  If Changing = True Then Exit Sub
  Changing = True
  For Each ctr In Me.Controls
    If TypeName(ctr) = "ToggleButton" And ctr.Name <> myToggle.Name Then ctr.Value = False
  Next
  Changing = False
End Sub

--------------
I hope to hear from you soon.
Respectfully
Dante Amor
--------------
Thank you Dante!
 
Upvote 1
OK, this time I'm 5 minutes late. Going to give up for tonight and watch baseball. The name being passed is case sensitive.
VBA Code:
Private Sub ToggleButton1_Click()
ToggleToggles ("ToggleButton1")
End Sub

Private Sub ToggleButton2_Click()
ToggleToggles ("ToggleButton2")
End Sub

Private Sub ToggleButton3_Click()
ToggleToggles ("ToggleButton3")
End Sub

Sub ToggleToggles(ctlName As String)
Dim ctl As control
For Each ctl In Me.Controls
     Debug.Print ctl.Name
     If TypeName(ctl) = "ToggleButton" Then If ctl.Name <> ctlName Then ctl = False
Next

End Sub
Haha, that's healthy competition! This code works as well, thank you!
 
Upvote 0
Well if it works like that then I just learned something. I've never written 2 if statements on one line like that. Not sure if the original is like that or not because I tend to edit after posting to remove extra spaces in the indents so I might have removed a line feed. I'll have to look later.
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,734
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top