Check state of a number of checkboxes

hstaubyn

Board Regular
Joined
Sep 13, 2010
Messages
93
Hi,

I have a spreadsheet in which I run a macro to insert checkboxes in some cells depending on the value of the next door cell. The name of each checkbox contains the row number. This works fine.

What I am trying to do now is write another one where each one is investigated to see if it is checked and, if so, store the number part of the checkbox name in an array. To do this I first ReDim the array to have the same number of elements as the number of 'TRUE' (checked) checkboxes on the worksheet. This is the code I am trying to use to count the number of TRUE check boxes.

Code:
Function Count_Positive_Checkboxes()
 
Dim Btn As Object
Dim PosCount As Long
 
PosCount = 0
 
For Each Btn In ActiveSheet.Shapes
    If Btn.Value = True Then
        PosCount = PosCount + 1
    End If
Next Btn
 
Count_Positive_Checkboxes = PosCount
 
End Function

The only objects on the worksheet are the checkboxes.

I get a run-time error 438 though with a message saying 'Object doesn't support this property or method'

Can anyone tell me what I'm doing wrong?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Since you are iterating the shapes collection, I'm assuming you are sure that these are form controls.

If my assumption is correct, please try this:

Code:
Function Count_Positive_Checkboxes()
 
Dim Btn As Shape
Dim PosCount As Long
 
PosCount = 0
 
For Each Btn In ActiveSheet.Shapes
    If Btn.ControlFormat.Value = Checked Then
        PosCount = PosCount + 1
    End If
Next Btn
 
Count_Positive_Checkboxes = PosCount
 
End Function

Gary
 
Upvote 0
You could also try it this way:

Code:
Public Sub Test()

Dim oCheckBox As CheckBox

For Each oCheckBox In ActiveSheet.CheckBoxes
    If oCheckBox.Value = 1 Then
        Debug.Print oCheckBox.Name & vbTab & "Checked"
    Else
        Debug.Print oCheckBox.Name & vbTab & "Un-Checked"
    End If
Next oCheckBox

End Sub

Gary
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,850
Members
452,948
Latest member
UsmanAli786

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