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

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Your code is looping through CheckBoxes named "ck_1" through "ck_10". And so you'll get that error if your worksheet does not contain a CheckBox with one of those names.
 
Upvote 0
Your code is looping through CheckBoxes named "ck_1" through "ck_10". And so you'll get that error if your worksheet does not contain a CheckBox with one of those names.
CheckBoxes (ck_1 through ck_10) are there. Now I´m setting their value to false one by one & it works fine. I want to make it better by using for loop.
 
Upvote 0
Your code refers to the active workbook, since you haven't qualified your sheet reference. Is the workbook in which the CheckBoxes are located in fact the active workbook when running the code?
 
Upvote 0
Your code refers to the active workbook, since you haven't qualified your sheet reference. Is the workbook in which the CheckBoxes are located in fact the active workbook when running the code?
Yes. Checkboxes are in the active workbook.

As mentioned before, when I use the following code separately for each of 10 checkboxes, it is working correctly. But when I use the for loop, it is throwing error.
VBA Code:
Worksheets("Sheet1").Ck_1.Value = False
 
Upvote 0
It looks like your CheckBoxes are from ActiveX controls, not Form controls. Try the following instead...

VBA Code:
Worksheets("Sheet1").OLEObjects("ck_" & i).Object.Value = False
 
Upvote 0
Solution
It looks like your CheckBoxes are from ActiveX controls, not Form controls. Try the following instead...

VBA Code:
Worksheets("Sheet1").OLEObjects("ck_" & i).Object.Value = False
Checkboxes are form controls only. Not ActiveX controls.
 
Upvote 0
Let's check whether or not your CheckBoxes are in fact Form controls. First, make sure that the workbook containing your CheckBoxes is the active workbook. Then enter the following line in the Immediate Window (Visual Basic Editor >> View >> Immediate Window), and press ENTER...

VBA Code:
? typeof Worksheets("Sheet1").Ck_1 is Excel.CheckBox

What does it return, True or False?
 
Upvote 0
Let's check whether or not your CheckBoxes are in fact Form controls. First, make sure that the workbook containing your CheckBoxes is the active workbook. Then enter the following line in the Immediate Window (Visual Basic Editor >> View >> Immediate Window), and press ENTER...

VBA Code:
? typeof Worksheets("Sheet1").Ck_1 is Excel.CheckBox

What does it return, True or False?
The code above is returning False.

I tried using the code suggested for ActiveX controls, but getting the run time error 438 (object doesn´t support this property or method)
error.PNG
 
Upvote 0
The code above is returning False.
Okay, so that confirms that it's not a Form control

I tried using the code suggested for ActiveX controls, but getting the run time error 438 (object doesn´t support this property or method)
In that case, try entering the following line in the Immediate Window...

VBA Code:
? typename(Worksheets("Sheet1").Ck_1)

What does it return?
 
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