Clear all option button backcolor on UserForm

jeffreybrown

Well-known Member
Joined
Jul 28, 2004
Messages
5,152
I have a Userform which when open on UserForm_Initialize will set the backcolor of OptionButton16 t vbYellow. When the user selects one of the other 15 OptionButtons, I would like that OptionButton to have a Backcolor of vbYellow, but the previous OptionButton is cleared of backcolor. Basically only on optionbutton clicked at a time with the backcolor.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try this:

In your userform, Initialize event:
VBA Code:
Dim OptBt() As New Class1         'At the start of all code

Private Sub UserForm_Initialize()
  Dim i As Long, ctrl As MSForms.Control
  i = 1
  
  OptionButton16.BackColor = vbYellow
  
  For Each ctrl In Me.Controls
    If TypeName(ctrl) = "OptionButton" Then
      ReDim Preserve OptBt(i)
      Set OptBt(i).MultOption = ctrl
      i = i + 1
    End If
  Next
End Sub

In a class module. Call "Class1"
Note: Change "Userform1" By the name of your userform
VBA Code:
Option Explicit

Public WithEvents MultOption As MSForms.OptionButton

Private Sub MultOption_Change()
  Dim ctrl As MSForms.Control
  
  For Each ctrl In UserForm1.Controls         'fit to the name of your userform
    If TypeName(ctrl) = "OptionButton" Then
      ctrl.BackColor = &H8000000F
    End If
  Next
  MultOption.BackColor = vbYellow
End Sub
 
Upvote 1
Solution
Thanks Dante. This works great for my needs but now I'd like to drop the caption of the optionbutton click into the activecell of my spreadsheet.

I've been trying this, but it debugs at the If statement.

VBA Code:
Private Sub CommandButton1_Click()
    Dim Ctrl As Control
    For Each Ctrl In Me.Controls
        If TypeName(Ctrl) = "OptionButton" And Ctrl.Value = True Then
            MsgBox Ctrl.Caption
            ActiveCell = Ctrl.Caption
        End If
    Next
End Sub
 
Upvote 0
Try

VBA Code:
Private Sub CommandButton1_Click()
    Dim Ctrl As Control
    For Each Ctrl In Me.Controls
        If TypeName(Ctrl) = "OptionButton" then
If Ctrl.Value = True Then
            MsgBox Ctrl.Caption
            ActiveCell = Ctrl.Caption
End if
        End If
    Next
End Sub
 
Upvote 1

Forum statistics

Threads
1,215,360
Messages
6,124,489
Members
449,166
Latest member
hokjock

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