loop thru checkboxes

mrinal saha

Board Regular
Joined
Jan 20, 2009
Messages
229
Hi

I have 4 checkboxes in sheet1. want to use a loop in workbook open event to uncheck the boxes if any. Well using the below code, but seems nothing happening.

For Each o In Sheets("Summary").OLEObjects
If TypeName(o.Object) = "checkbox" Then
If o = True Then
o = False
End If
End If
Next o


thankssss,
Mrinal
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I have 4 checkboxes in sheet1. want to use a loop in workbook open event to uncheck the boxes if any. Well using the below code, but seems nothing happening.

For Each o In Sheets("Summary").OLEObjects
If TypeName(o.Object) = "checkbox" Then
If o = True Then
o = False
End If
End If
Next o

Hi Mrinal,

There are 2 problems with this code;

1) TypeName(o.Object) returns "CheckBox", which is typographically not equal to "checkbox", so you need to compare it not just with a simple "=". Instead use:
Code:
If LCase(TypeName(o.Object)) = "checkbox" Then
or:
Code:
If StrComp(TypeName(o.Object)), "checkbox", vbTextCompare) = 0 Then
but a more straight-forward method is using:
Code:
If TypeOf o.Object Is MSForms.CheckBox Then
That should take care of selecting only checkboxes.

2) Setting your o directly doesn't do what you think it does. You need to again use o.Object, since .Object contains the control itself. And then tack on an extra .Value to explicitly access the value of it. Which makes:
Code:
If o.Object.Value = True Then

Hope this helps!
 
Upvote 0
They're ActiveX controls, yes? Try:-
Code:
  For Each o In Sheets("Summary").OLEObjects
  If TypeName(o.Object) = "[COLOR=red][B]C[/B][/COLOR]heck[COLOR=red][B]B[/B][/COLOR]ox" Then
     If o[COLOR=red][B].Object[/B][/COLOR] = True Then
        o[COLOR=red][B].Object[/B][/COLOR] = False
     End If
  End If
Next o
 
Upvote 0

Forum statistics

Threads
1,224,548
Messages
6,179,445
Members
452,915
Latest member
hannnahheileen

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