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

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
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,223,896
Messages
6,175,263
Members
452,627
Latest member
KitkatToby

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