I've not posted to a forum for Excel previously, normally my problems are solved by past answers or persistence, but this is a strange one.
I have a chart in Excel where series can be added and removed dynamically using check boxes. It works perfectly in Excel 2010, however in Excel 2016 the application stops responding and crashes when removing the series, adding a series still works correctly.
Any ideas? Is anybody aware of SeriesCollection.Delete not working in 2016?
I have a chart in Excel where series can be added and removed dynamically using check boxes. It works perfectly in Excel 2010, however in Excel 2016 the application stops responding and crashes when removing the series, adding a series still works correctly.
Any ideas? Is anybody aware of SeriesCollection.Delete not working in 2016?
Code:
Private Sub chkSalesTotMtd_Change()
Dim sheet As String
Dim chtName As String
Dim chkValue As Variant
Dim chkBox As String
Dim linename As String
Dim valuefield As String
Dim linenameabr As String
sheet = "ChtDealerMonth"
chkBox = "chkSalesTotMtd"
linename = "=""Sales Total Mtd"""
linenameabr = "Sales Total Mtd"
valuefield = "=BalanceSplit.xlsm!SalesTotMtdDlr"
Call unprotectsheetsdlr
Dim objChart As ChartObject
For Each objChart In Sheets("ChtDealerMonth").ChartObjects
chtName = objChart.Name
Call addnewseriessecond(sheet, chkBox, chtName, linename, linenameabr, valuefield)
Next objChart
Call protectsheetsdlr
End Sub
Code:
Function addnewseries(sheet As String, chkBox As String, chtName As String, linename As String, linenameabr As String, valuefield As String)
ActiveSheet.Unprotect
chkValue = Sheets(sheet).OLEObjects(chkBox).Object.Value
ActiveSheet.ChartObjects(chtName).Activate
If chkValue = True Then
'if checkbox is ticked
Dim NewSrs As Series
Set NewSrs = ActiveChart.SeriesCollection.NewSeries
'add new series to chart
With NewSrs
.Name = linename
.Values = valuefield
.ChartType = xlLine
.Format.Line.Weight = 2.25
End With
ElseIf chkValue = False Then
'if checkbox unticked
Dim IndexNum As Integer
Set cht = ActiveChart.SeriesCollection
'find index of series which matches name of series to remove
For i = 1 To cht.Count
If cht(i).Name = linenameabr Then IndexNum = i
Next i
'delete series from chart
cht(IndexNum).Delete
End If
End Function