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
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
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
those check box in userform or sheet?
 
Upvote 0
first you need create new "Class Module" with name "Class1" then place this code into it:
VBA Code:
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

next, create new module and place this code at first:
VBA Code:
Option Explicit

Private newCheckBox() As New Class1

Sub CheckBoxControl()
    Dim cb As Control
    Dim i As Integer
    ReDim newCheckBox(1000)
    For Each cb In UserForm1.Controls 'change UserForm1 to your userform name
        If TypeOf cb Is MSForms.CheckBox Then
            Set newCheckBox(i).newChB = cb
            i = i + 1
        End If
    Next cb
End Sub

and final is call CheckBoxControl in Initialize Event of your userform:
VBA Code:
Private Sub UserForm_Initialize()
    Call CheckBoxControl
End Sub
 
Upvote 0
first you need create new "Class Module" with name "Class1" then place this code into it:
VBA Code:
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

next, create new module and place this code at first:
VBA Code:
Option Explicit

Private newCheckBox() As New Class1

Sub CheckBoxControl()
    Dim cb As Control
    Dim i As Integer
    ReDim newCheckBox(1000)
    For Each cb In UserForm1.Controls 'change UserForm1 to your userform name
        If TypeOf cb Is MSForms.CheckBox Then
            Set newCheckBox(i).newChB = cb
            i = i + 1
        End If
    Next cb
End Sub

and final is call CheckBoxControl in Initialize Event of your userform:
VBA Code:
Private Sub UserForm_Initialize()
    Call CheckBoxControl
End Sub
with this code, you can lock and unlock all checkboxes except that you clicked
 
Upvote 0
with this code, you can lock and unlock all checkboxes except that you clicked
Thank you for this... :) What if i have several/many userforms?


Option Explicit

Private newCheckBox() As New Class1

Sub CheckBoxControl()
Dim cb As Control
Dim i As Integer
ReDim newCheckBox(1000)
For Each cb In UserForm1.Controls 'change UserForm1 to your userform name
If TypeOf cb Is MSForms.CheckBox Then
Set newCheckBox(i).newChB = cb
i = i + 1
End If
Next cb
End Sub
 
Upvote 0
then
Thank you for this... :) What if i have several/many userforms?


Option Explicit

Private newCheckBox() As New Class1

Sub CheckBoxControl()
Dim cb As Control
Dim i As Integer
ReDim newCheckBox(1000)
For Each cb In UserForm1.Controls 'change UserForm1 to your userform name
If TypeOf cb Is MSForms.CheckBox Then
Set newCheckBox(i).newChB = cb
i = i + 1
End If
Next cb
End Sub
then change it like this:
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

and
VBA Code:
Private Sub UserForm_Initialize()
    Call CheckBoxControl(Me)
End Sub

to do it for each userform, you should call "CheckBoxControl" for all Initialize event of each userform
 
Upvote 0
then

then change it like this:
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

and
VBA Code:
Private Sub UserForm_Initialize()
    Call CheckBoxControl(Me)
End Sub

to do it for each userform, you should call "CheckBoxControl" for all Initialize event of each userform
What will be my program for Class module?
 
Upvote 0
What will be my program for Class module?
still the same, if all your user forms in same file then just need call CheckBoxControl for each user form, only need 1 class module
 
Upvote 0
still the same, if all your user forms in same file then just need call CheckBoxControl for each user form, only need 1 class module
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
 
Upvote 0

Forum statistics

Threads
1,215,076
Messages
6,122,987
Members
449,093
Latest member
Mr Hughes

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