Relative Referencing Control Names when using ActiveX Checkbox Control

WilliamAnton

New Member
Joined
Feb 23, 2015
Messages
6
I chose to use ActiveX Checkbox Controls. While I like the formatting options I'm not liking my inability to reference the control name in the code so that I don't have to duplicate the same code from row to row (with each row having a corresponding calculation impact on a associated data field. After hours and hours of researching and reading I'm not finding a way to do the equivalent of the activesheets logic available in the Form Control objects. Is there a coding solution for this? Thank you in advance. I'm losing my mind and as I'm not the most technical person here, there's not much left to lose.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
I understand the controls are on the worksheet, not inside a user form. Is this correct?

Code:
' sheet module
Private Sub Worksheet_Activate()
Dim i%, olb As OLEObject
For i = 1 To Me.OLEObjects.Count
    Set olb = Me.OLEObjects(i)                                              ' by index
    If InStr(olb.Name, "Check") > 0 Then MsgBox "Name: " & olb.Name & _
    vbLf & "Location: " & olb.TopLeftCell.Address
Next
MsgBox Me.OLEObjects("CheckBox1").Object.Value, 64, "Check Box Status"      ' by name
End Sub
 
Upvote 0
Thank you for the quick response. Yes, the objects are on a worksheet. I'll be getting back to my PC in a couple of hours. Dinner time here.

Please forgive my basic understandings. I'm trying to move from "form controls" to ActiveX and I'm lost. I get the layering of the object model conceptually, but really struggling with coding.

I'll add your code when I get home. Is there a street level explanation of what outcome I will be getting? Is this code listing the addresses of the objects? I was hoping to write a function that I could call that put the Active Control name automagically into generic code that would also pull the relative data & columns I'm acting on.

Thank you again for your kindness.
 
Upvote 0
When the sheet is activated, the code will give you the names and worksheet position of all controls that have the word “check” in their names. This part is done referencing the controls by sequential indexing (1, 2, 3...)

Cell position is useful when you want to manipulate data on the same row (or any other relative row or column, actually)
After that, the code informs if one check box is selected, this time referencing it by name.

To ease the programming task, I would suggest you name them CheckBox1, CheckBox2… although this is not mandatory.
If you provide more details on what you want to accomplish, I can write more example code.
 
Upvote 0
Hello Worf. Thanking you again for your help. Getting the following error. "Compile error. Invalid use of Me keyword." I copied your code into a Button as follows:

Sub Worksheet_Activate_Click()


Dim i%, olb As OLEObject
For i = 1 To Me.OLEObjects.Count
Set olb = Me.OLEObjects(i) ' by index
If InStr(olb.Name, "Check") > 0 Then MsgBox "Name: " & olb.Name & _
vbLf & "Location: " & olb.TopLeftCell.Address
Next
MsgBox Me.OLEObjects("CheckBox1").Object.Value, 64, "Check Box Status" ' by name


End Sub

<hr/>

I don't know how much more effort to put into these ActiveX Checkbox Controls. I converted the Checkboxes in my spreadsheet to Forms Control Checkboxes and got the functionality I needed. I'm such a rookie at this it took a bit, but it's working. I still have an interest in learning ActiveX coding in the long run. I'd like to understand these types of errors, but under time pressures to finish this project up right now. I'm off to coding similar functionality for Forms Control Comboboxes. wish me luck.
 
Upvote 0
The code below worked for me. If you need help with form controls, post here.

Code:
' sheet module
Private Sub CommandButton1_Click()                                      ' ActiveX button
Dim i%, olb As OLEObject
For i = 1 To Me.OLEObjects.Count
    Set olb = Me.OLEObjects(i)                                          ' by index
    If InStr(olb.Name, "Check") > 0 Then MsgBox "Name: " & olb.Name & _
    vbLf & "Location: " & olb.TopLeftCell.Address
Next
MsgBox Me.OLEObjects("CheckBox1").Object.Value, 64, "Check Box Status"  ' by name
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

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