Loop through option buttons in VBA

Lenka

New Member
Joined
Jun 22, 2022
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Hello, I need your help with form controls in VBA.
I want to write a code that would reset to false the option buttons dependent on previous one. I need something like this: If the option button 1 is selected then turn off and make invisible option button 25-58.
Problem is I have a lot of option buttons in the sheet and the numerical order in name is not allways continuous as I needed to delete some option buttons when making the form.
Is there a way to detect them so I will not have to write millions of codes for every option button separately?
Thank you for your advise :)
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Are your option buttons in a UserForm?
If they are in a worksheet, are they Forms controls or ActiveX controls?

The code will be different in each of these situations, but here is one example where they are in a UserForm:
VBA Code:
Private Sub OptionButton1_Click()

   Dim i As Long
   Dim C As Control
   
   If OptionButton1 Then
   
      On Error Resume Next
      For i = 25 To 58
      
         Set C = Controls("OptionButton" & i)
         
         If Err.Number = 0 Then
            C.Value = False
            C.Visible = False
         Else
            Err.Clear
         End If
      
      Next i
      
   End If

End Sub
 
Upvote 0
Are your option buttons in a UserForm?
If they are in a worksheet, are they Forms controls or ActiveX controls?

The code will be different in each of these situations, but here is one example where they are in a UserForm:
VBA Code:
Private Sub OptionButton1_Click()

   Dim i As Long
   Dim C As Control
  
   If OptionButton1 Then
  
      On Error Resume Next
      For i = 25 To 58
     
         Set C = Controls("OptionButton" & i)
        
         If Err.Number = 0 Then
            C.Value = False
            C.Visible = False
         Else
            Err.Clear
         End If
     
      Next i
     
   End If

End Sub
I am not using UserFom. They are in a worksheet and they are form controls.
 
Upvote 0
Assign this macro to Option Button 1:
VBA Code:
Public Sub OptionButton1_Click()

   Dim i As Long
   Dim C As Control
   
   Debug.Print ActiveSheet.Shapes("Option Button 1").ControlFormat.Value
   
   
   If ActiveSheet.Shapes("Option Button 1").ControlFormat.Value = xlOn Then

      On Error Resume Next
      For i = 2 To 6 ' 25 To 58
         ActiveSheet.Shapes("Option Button " & i).ControlFormat.Value = xlOff
         If Err.Number = 0 Then
            ActiveSheet.Shapes("Option Button " & i).Visible = False
         Else
            Err.Clear
         End If

      Next i

   End If
   
End Sub
 
Upvote 0
Solution
Assign this macro to Option Button 1:
VBA Code:
Public Sub OptionButton1_Click()

   Dim i As Long
   Dim C As Control
  
   Debug.Print ActiveSheet.Shapes("Option Button 1").ControlFormat.Value
  
  
   If ActiveSheet.Shapes("Option Button 1").ControlFormat.Value = xlOn Then

      On Error Resume Next
      For i = 2 To 6 ' 25 To 58
         ActiveSheet.Shapes("Option Button " & i).ControlFormat.Value = xlOff
         If Err.Number = 0 Then
            ActiveSheet.Shapes("Option Button " & i).Visible = False
         Else
            Err.Clear
         End If

      Next i

   End If
  
End Sub
Thank you very much, that worked :) The error codes were what I was missing in my code.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,684
Members
448,977
Latest member
dbonilla0331

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