Looping thru Checkboxes in a worksheet

sharshra

Active Member
Joined
Mar 20, 2013
Messages
404
Office Version
  1. 365
I have to loop thru check boxes in a worksheet & was searching for a solution. I found the following post from mrexcel excel questions forum. Post #2 is exactly what I need.
VBA Code:
    Dim i As Long
    
    For i = 1 To 10
        Worksheets("Sheet1").CheckBoxes("ck_" & i).Value = False
    Next i
But, when I use this, I´m getting an error shown below. Not sure what is going wrong. Can the experts in this forum help please?

Link to a post from mrexcel excel questions forum:
[VBA] loop through all check box buttons on a sheet

Error:
loop checkbox.PNG
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
So far we've determined that it's not a Form control, but it is a CheckBox. So it must be an ActiveX control. And we should be able to confirm this with the following...

VBA Code:
? typeof worksheets("Sheet1").ck_1 is MSForms.CheckBox

It should return True. Does it?

Also, try fully qualifying your reference. For example, if the workbook running the code contains the CheckBoxes...

VBA Code:
ThisWorkbook.Worksheets("Sheet1").OLEObjects("ck_" & i).Object.Value = False

Otherwise...

VBA Code:
 Workbooks("Book2.xlsx").Worksheets("Sheet1").OLEObjects("ck_" & i).Object.Value = False
 
Upvote 0
Thanks Domenic for your advice so far. Yes, the following returns True.
VBA Code:
? typeof worksheets("Sheet1").ck_1 is MSForms.CheckBox

But, I have problem with next set of codes to assign a value to CheckBox. When the code is executed, I´m getting the error shown below in the following line.
Code:
thisworkbook.Worksheets ("Sheet1").oleobjects("ck_" & i).object.value = False

1711031810472.png


In the immediate window, I get the correct results for checkbox. For example, following code in immediate window show me the current value (true or false) of checkbox.
Code:
?thisworkbook.Worksheets ("Sheet1").oleobjects("ck_1").object.value
 
Upvote 0
Hmmm... this is very perplexing. If that line of code succeeds in the Immediate Window, then your other line should succeed as well. The only way I get that error is if I omit .Object from the reference. Maybe it might help if you posted your complete code.
 
Upvote 0
Yes, it is puzzling :confused:
My code is simple. I´m just using a For loop to assign False to checkbox Value.
VBA Code:
For i = 1 To 10
    ThisWorkbook.Worksheets("Sheet1").OLEObjects("ck_" & i).Object.Value = False
Next i

The code returns correct value in immediate window but failing in runtime :rolleyes:
Code:
?ThisWorkbook.Worksheets("Sheet1").OLEObjects("ck_" & i).Object.Value
 
Upvote 0
Do you have any oleobjects in the worksheet other than the checkboxes and which have a similar ck_i name pattern?

EDIT:
Try running this to find out what is in the worksheet:
VBA Code:
    Dim oObj As Object
    For Each oObj In ThisWorkbook.Worksheets("Sheet1").OLEObjects
        Debug.Print oObj.Name, TypeName(oObj.Object)
    Next oObj
 
Upvote 0
No. I have only checkboxes in the worksheet. There are no other controls.

When I run the code mentioned above, I see only ck_1 to 10.
 
Upvote 0
Yes, it is puzzling :confused:
My code is simple. I´m just using a For loop to assign False to checkbox Value.
VBA Code:
For i = 1 To 10
    ThisWorkbook.Worksheets("Sheet1").OLEObjects("ck_" & i).Object.Value = False
Next i

The code returns correct value in immediate window but failing in runtime :rolleyes:
Code:
?ThisWorkbook.Worksheets("Sheet1").OLEObjects("ck_" & i).Object.Value
Does the code fail at the first iteration? ie:= when i =1 or at subsequent calls ? Also, do you have any sheet protection in place?
 
Upvote 0
It is failing in the first instance itself. Sheet is not protected.

I also tried using checkbox name outside the For loop, but still getting the same error.
VBA Code:
ThisWorkbook.Worksheets("Sheet1").OLEObjects("ck_1").Object.Value = False
 
Upvote 0
Try running this code and see on which line the runtime error occurs: ( and tell us exactly which error if any)

VBA Code:
Dim v As Variant

Set v = ThisWorkbook.Worksheets("Sheet1")
Set v = ThisWorkbook.Worksheets("Sheet1").OLEObjects
Set v = ThisWorkbook.Worksheets("Sheet1").OLEObjects("ck_1")
Set v = ThisWorkbook.Worksheets("Sheet1").OLEObjects("ck_1").Object
v = ThisWorkbook.Worksheets("Sheet1").OLEObjects("ck_1").Object.Value
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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