0

I am trying to create something like a "Mastercheckbox" that will automatically check all other checkboxes on my worksheet. So I'm trying to start the following VBA Code as soon as I click click on the "Mastercheckbox" I am using Form Tools rather than Active X to create my checkboxes in order to make sure my VBA is compatible with any machine (I read that Active X might cause some issues) and have got the following code:

Sub Mastercheckbox()

Dim Chk As CheckBox

With ActiveSheet.Shapes("Mastercheckbox")
    If .ControlFormat.Value = False Or .ControlFormat.Value = True Then
        If .ControlFormat.Value = False Then
            For Each chk In ActiveSheet.CheckBoxes
                If Not chk.Name = "Mastercheckbox" Then
                chk.Value = False
                End If
            Next chk
        End If

        If Not .ControlFormat.Value = True Then
            For Each chk In ActiveSheet.CheckBoxes
                If Not chk.Name = "Mastercheckbox" Then
                chk.Value = True
                End If
            Next chk
        End If
     Else: MsgBox ("couldn't check value of mastercheckbox")
     End If
End With
End Sub

Everything in the For Loops works, but checking the value of the Mastercheckbox just isn't working for some reason and it jumps straight to the Else case. Can anyone help me out?

2 Answers 2

3

being not an ActiveX control you must check its value against xlOn(corresponding to 1) and xlOff `(corresponding to -4146)

you may also refactor a little your code as follows:

Option Explicit

Sub Mastercheckbox()        
    With ActiveSheet.Shapes("Mastercheckbox")
        If .ControlFormat.Value = xlNone Then
            MsgBox ("couldn't check value of mastercheckbox")
        Else
            HandleCheckBoxes .ControlFormat.Value
        End If
    End With
End Sub

Sub HandleCheckBoxes(val As Long)
    Dim Chk As CheckBox

    For Each Chk In ActiveSheet.CheckBoxes
        If Not Chk.name = "Mastercheckbox" Then Chk.Value = val
    Next Chk
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

You are welcome. See my edit to shorten the code a little
1

Since you have decided to use User_Form checkbox, you need to define your variables as Shape. And in order to check their value, you need to check if they are xlOn or xlOff.

In order to see that you read the value of "Mastercheckbox" once it's pressed, you can use the piece of code below:

Dim MasterCB As Shape

Set MasterCB = ActiveSheet.Shapes("Mastercheckbox") '<-- set the MasterCB  variable

If MasterCB.ControlFormat.Value = xlOn Then
    MsgBox "Is checked"
ElseIf MasterCB.ControlFormat.Value = xlOff Then
    MsgBox "Not checked"
End If

Code for your post

Option Explicit

Sub Mastercheckbox()

Dim Chk As Shape
Dim MasterCB As Shape

' set the MasterCB  variable
Set MasterCB = ActiveSheet.Shapes("Mastercheckbox")

' loop through all shapes in ActiveSheet
For Each Chk In ActiveSheet.Shapes
    If Chk.Type = msoFormControl Then '<-- check your shape type is User_Form type
        If Chk.FormControlType = xlCheckBox Then '<-- check if the shape is a checkbox
            If Chk.Name <> "Mastercheckbox" Then 
                Chk.ControlFormat.Value = MasterCB.ControlFormat.Value '<-- modify all checkboxes to the value of "Mastercheckbox"
            End If
        End If
    End If
Next Chk

End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.