8

When adding a checkbox, how do you access the value from VBA?

  • In Excel 2007, on the Developer Ribbon
  • Insert, Form Controls, Checkbox
  • Renamed Checkbox to chkMyCheck
  • Added Macro to checkbox, I now have Module1 with chkMyCheck_Clicked

All of the following fail

Sheets("Sheet1").chkMyCheck.Checked  
Sheets("Sheet1").chkMyCheck.Value  
Sheets("Sheet1").Shapes("chkMyCheck").Checked  
Sheets("Sheet1").Shapes("chkMyCheck").Value  
Sheet1.chkMyCheck.Checked  
Sheet1.chkMyCheck.Value  

Sheet1.Shapes("chkMyCheck") appears to find the object, but does not expose any properties that look likely for returning the checked state.

3 Answers 3

9

Figured it out

If Sheet1.Shapes("chkMyCheck").ControlFormat.Value = xlOn Then
.....
Sign up to request clarification or add additional context in comments.

Comments

4

One way:

Dim oCheck As Object
Set oCheck = Sheet1.CheckBoxes("chkMyCheck")
MsgBox (oCheck.Value = xlOn)

Edit: here's another method - maybe this one will work for you...

Sub Tester2()
    Dim sh As Shape
    For Each sh In Sheet1.Shapes
        If sh.Type = msoFormControl Then
            If sh.FormControlType = xlCheckBox Then
                 Debug.Print sh.Name & "=" & sh.ControlFormat.Value
            End If
        End If
    Next sh
End Sub

3 Comments

Also does not work. There's no CheckBoxes property on a sheet in Excel 2007 (at least not my copy).
Tested fine for me: Form controls checkbox, Excel 2007 on Windows XP & 7. It's true there's no "CheckBoxes" collection in the Object Browser, but that's not stopping it working.
If you right click in the Object Browser and choose Show Hidden Members, you will see Checkbox and Checkboxes. They're deprecated, but still in there.
2

For completeness, if you're using an ActiveX checkbox instead of a regular checkbox, the syntax is

If Sheet1.Shapes("chkMyCheck").OLEFormat.Object.Object.Value Then 
...

found using the Locals window and a variable set to the shape -

Dim shp as Shape
Set shp = Sheet1.Shapes("chkMyCheck")
Stop

1 Comment

Likewise Sheet1.Shapes("chkMyCheck").OLEFormat.Object.Object.Value = True enables an ActiveX option button.

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.