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
 
bs0d

Yes, that's what I was thinking.

I also tried passing the actual chart/chartobject but couldn't get that to work.
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Maybe...

Code:
Public Function chkClick(oOleoObj As OLEObject, oChart As Chart, seriesNumber As Long)

    If TypeName(oOleoObj.Object) = "CheckBox" Then
        If oOleoObj.Object.Value = True Then
            oChart.FullSeriesCollection(seriesNumber).Format.Line.Visible = msoTrue
        Else
            oChart.FullSeriesCollection(seriesNumber).Format.Line.Visible = msoFalse
        End If
    End If

End Function

Function call:

Code:
chkClick Sheets("Sheet 1").OLEObjects("chkTest"), Sheets("Sheet 1").ChartObjects("myChart").Chart, 1
 
Upvote 0
Domenic

I tried it in a similar way but couldn't get it to work, probably because I know next to nothing about manipulating charts in code.

Actually, I've just had another look at your code and I think I see where I was going wrong - never thought of using the Visible property.:)
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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