Looping through checkboxes

J

jkrons

I have the line of code

Sheets(1).OLEObjects("CheckBox1").Object.Value = True

that works great, but how do I loop through all the checkboxes in my
sheet with something like this

For Each cb In Sheets(1).OLEObjects.CheckBoxes
Sheets(1).OLEObjects(cb.Name).Object.Value = True

I get a runtime error 430 Object doesn't support this property or
method.

How do I loop through the checkboxes?
 
J

John Coleman

I have the line of code

Sheets(1).OLEObjects("CheckBox1").Object.Value = True

that works great, but how do I loop through all the checkboxes in my
sheet with something like this

For Each cb In Sheets(1).OLEObjects.CheckBoxes
Sheets(1).OLEObjects(cb.Name).Object.Value = True

I get a runtime error 430 Object doesn't support this property or
method.

How do I loop through the checkboxes?

I don't know of any way to do this directly. If you keep the default
control names (or consistently adopt a naming convention) you could
use the like operator:

Sub test()
Dim control As OLEObject

For Each control In Sheets(1).OLEObjects
If control.Name Like "CheckBox*" Then MsgBox control.Name
Next

End Sub

hth
 
J

jkrons

I don't know of any way to do this directly. If you keep the default
control names (or consistently adopt a naming convention) you could
use the like operator:

Sub test()
    Dim control As OLEObject

    For Each control In Sheets(1).OLEObjects
        If control.Name Like "CheckBox*" Then MsgBox control.Name
    Next

End Sub

hth

Thank you. That helped.

Jan
 
G

Gord Dibben

For Each mychkbox In wks.CheckBoxes
With mychkbox
.Value = True
End With
Next mychkbox


Gord
 
J

John Coleman

For Each mychkbox In wks.CheckBoxes
        With mychkbox
          .Value = True
        End With
    Next mychkbox

Gord

That seems to work for form controls but not ActiveX controls. It also
seems to be poorly documented (e.g. doesn't appear in the object
browser for worksheet methods) so thanks for pointing it out.
 
G

Gord Dibben

You are correct John.

Forms only............was not paying attention(usual state).

Did not notice OP required code on Activex Checkboxes.

The code I posted was part of a larger set by Dave Peterson for
changing Forms checkbox linked cells.


Gord
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top