VBA Custom Function - Form Control Name Object not Supported?

bs0d

Well-known Member
Joined
Dec 29, 2006
Messages
622
I'm creating a custom function to cut down on redundant code. However, in converting the code to a function so that it may be used in different instances, I'm getting an unexpected error when the code executes.

The error is:

Code:
Object doesn't support this property or method.

Here is my function:

Code:
Public Function chkClick(checkboxName As String, tabName As String, chartName As String, seriesNumber As Long)

Sheets(tabName).ChartObjects(chartName).Activate
    
If Sheets(tabName).checkboxName.Value = True Then
    
    ActiveChart.FullSeriesCollection(seriesNumber).Select
    With Selection.Format.Line
        .Visible = msoTrue
    End With
Else
    ActiveChart.FullSeriesCollection(seriesNumber).Select
    With Selection.Format.Line
        .Visible = msoFalse
    End With

End If

End Function

The function checks the status of the checkbox name passed to it. If it's checked, it will show a chart series. If it's not checked, it will hide the chart series. The error comes in referencing the "checkboxName".

How might I need to alter the code so that I can use the form control name as a variable? This is ultimately one of the key components of the function as each control has it's own name.

Thanks
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Where is your Checkbox from, ActiveX or Form Controls? You can't use a string as the explicit name of an object.
 
Upvote 0
What type of checkbox do you want to pass to the function?
 
Upvote 0
Try...

Code:
If Sheets(tabName).CheckBoxes(checkboxName).Value = True Then

Hope this helps!
 
Upvote 0
Last edited:
Upvote 0
Sorry all, it's an ActiveX checkbox.

Domenic, I tried your code with no luck, perhaps because it's ActiveX

I think you mentioned "form control" in your post, hence my code. For an ActiveX checkbox, try...

Code:
If Sheets(tabName).OLEObjects(checkboxName).Object.Value = True Then

Hope this helps!
 
Upvote 0
Why not pass the checkbox itself to the function?
 
Upvote 0
Why not pass the checkbox itself to the function?

Here is the working code:

Code:
Public Function chkClick(checkboxName As OLEObject, tabName As String, chartName As String, seriesNumber As Long)

Sheets(tabName).ChartObjects(chartName).Activate
    
If checkboxName.Object.Value = True Then
    
    ActiveChart.FullSeriesCollection(seriesNumber).Select
    With Selection.Format.Line
        .Visible = msoTrue
    End With
Else
    ActiveChart.FullSeriesCollection(seriesNumber).Select
    With Selection.Format.Line
        .Visible = msoFalse
    End With

End If

End Function

function call:
Code:
chkClick Sheets("Sheet 1").OLEObjects("chkTest"), "Sheet 1", "myChart", 1

Norie - Is this solution what you were referring to?

Domenic, sorry about the name reference as a form control. I was thinking of it as a form control and wasn't specific in the initial post that it was ActiveX.

Thanks Mr. Poulsom, that's the code I used along with defining the function parameter as an OLEObject.
 
Upvote 0

Forum statistics

Threads
1,216,444
Messages
6,130,659
Members
449,585
Latest member
Nattarinee

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