How to lock multiple checkboxes

anthonymedz

Board Regular
Joined
Jan 30, 2015
Messages
69
Hi,

I would like to ask how to lock multiple checkboxes in VBA instead of manually locking the checkboxes one by one?

Private Sub CheckBox1_Click()
Sheets("Sheet1").Visible = True
Sheets("Sheet1").Select

'CheckBox1.Locked = CheckBox1.Value
CheckBox2.Locked = CheckBox1.Value
CheckBox3.Locked = CheckBox1.Value
CheckBox4.Locked = CheckBox1.Value
CheckBox5.Locked = CheckBox1.Value
.
.
.
.
CheckBox999.Locked = CheckBox1.Value
 
This will be my 1 class module?

Option Explicit

Public WithEvents newChB As MSForms.CheckBox

Private Sub newChB_Change()
Dim cb As Control
For Each cb In UserForm1.Controls
If TypeOf cb Is MSForms.CheckBox Then
If Not cb Is newChB Then cb.Locked = newChB.Value
End If
Next cb
End Sub
you need to change UserForm1 to cb.parent
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
sorry, it has some mistaken:
at class1:
VBA Code:
Option Explicit

Public WithEvents newChB As MSForms.CheckBox

Private Sub newChB_Change()
    Dim cb As Control
    For Each cb In newChB.Parent.Controls
        If TypeOf cb Is MSForms.CheckBox Then
            If Not cb Is newChB Then cb.Locked = newChB.Value
        End If
    Next cb
End Sub
at nomal module:
VBA Code:
Option Explicit

Private newCheckBox() As New Class1

Sub CheckBoxControl(ByVal uf As UserForm)
    Dim cb As Control
    Dim i As Integer
    ReDim newCheckBox(1000)
    For Each cb In uf.Controls
        If TypeOf cb Is MSForms.CheckBox Then
            Set newCheckBox(i).newChB = cb
            i = i + 1
        End If
    Next cb
End Sub
in userform:
VBA Code:
Private Sub UserForm_Initialize()
    Call CheckBoxControl(Me)
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,964
Members
449,094
Latest member
Anshu121

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