I have a userform with some checkboxes
In standard module I have this code
Code: Select all
Dim xCollection As New Collection
Public Sub ClsChk_Init()
'Dim xSht As Worksheet
Dim xObj As Object
Dim xChk As ClsChk
'Set xSht = ActiveSheet
Set xCollection = Nothing
For Each xObj In UserForm1.Controls
If xObj.Name Like "CheckBox**" Then
Set xChk = New ClsChk
Set xChk.Chk = CallByName(UserForm1, xObj.Name, VbGet)
xCollection.Add xChk
End If
Next
Set xChk = Nothing
UserForm1.Show
End Sub
Code: Select all
Public WithEvents Chk As MSForms.CheckBox
Private Sub Chk_Click()
Call SelOneCheckBox(Chk)
MsgBox "OK"
End Sub
Private Sub Chk_Change()
Dim ctl As MSForms.Control
If Chk.Value = True Then
For Each ctl In UserForm1.Controls
If TypeName(ctl) = "CheckBox" And Chk.Name <> ctl.Name Then
ctl.Vaue = False
End If
Next ctl
End If
End Sub
Sub SelOneCheckBox(Target As Object)
Dim xObj As Object
Dim I As String
Dim n As Integer
If Target.Object.Value = True Then
I = Right(Target.Name, Len(Target.Name) - 8)
For n = 1 To ActiveSheet.OLEObjects.Count
If n <> Int(I) Then
Set xObj = ActiveSheet.OLEObjects.Item(n)
xObj.Object.Value = False
xObj.Object.Enabled = False
End If
Next
Else
I = Right(Target.Name, Len(Target.Name) - 8)
For n = 1 To ActiveSheet.OLEObjects.Count
If n <> Int(I) Then
Set xObj = ActiveSheet.OLEObjects.Item(n)
xObj.Object.Enabled = True
End If
Next
End If
End Sub
What I am trying to achive through the class module is the check only one checkbox, in other words if there is a checked checkbok and I checked another one then to remove all the checks from all other checkboxes.