How to loop through ActiveX Controls placed on sheet

szita2000

Board Regular
Joined
Apr 25, 2012
Messages
101
Office Version
  1. 365
Platform
  1. Windows
Hi guys.
I am building a loop that will filter certain things within a date range.
I have to put some basic controls on the worksheet where the data is.
In this fashion.
1656925236176.png


What I'm trying to do is to disable the optionbuttons when the user enters a date in to the textboxes (I did not use datepicker, as I had trouble registering them on other PCs)

I would like to loop through the controls, however I can't get to them
What is the correct syntax to do this?
When I debug It gives me a type mismatch error highlighting the "OptionButton" bit.

VBA Code:
Option Explicit

Sub test()

    Dim ctrl As OLEObject

    
        For Each ctrl In Worksheets("CL Check List File").OLEObject
            Debug.Print ctrl.Name
            If TypeName(ctrl) Is "OptionButton" Then

                ctrl.Enabled = False
            End If

        Next ctrl

End Sub

Thanks

Thomas
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
TypeName returns a string, so try using
VBA Code:
If TypeName(ctrl) = "OptionButton" Then
 
Upvote 0
Solution
TypeName returns a string, so try using
VBA Code:
If TypeName(ctrl) = "OptionButton" Then
Thanks!

Now it stops at the first line of the For loop.
Error 438 - Object Doesn't support this property or method.
Maybe I can't operate with OLEObjects on a worksheet?
Solved it! It was the wrong syntax!!!

Instead of:

VBA Code:
For Each ctrl In Worksheets("CL Check List File").OLEObject

I needed

For Each ctrl In Worksheets("CL Check List File").OLEObjects 'As it is a collection of controls.

Thanks a lot Anthony47!
 
Last edited:
Upvote 0
Just for good measure.

The correct Syntax that works is:

VBA Code:
Sub test()

    Dim ctrl As OLEObject

    
        For Each ctrl In Worksheets("CL Check List File").OLEObjects
            Debug.Print ctrl.progID
            If ctrl.progID = "Forms.OptionButton.1" Then

                ctrl.Enabled = False
            End If
        
        
        
        Next ctrl

End Sub
 
Upvote 0
Google, try, google, retry; that's the path to get it running...
...and thank you for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,270
Members
452,628
Latest member
dd2

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