Error Handling Does Catch Error

Temo98

New Member
Joined
Oct 23, 2009
Messages
45
Good Day All -

I have written the following code to update the data series within a chart based on the selectiona user makes in a drop down box. At times tand issue will occur where the series gets delted and an error will occur when I go to set the series values as the series has been deleted, thus the values cant be set. I have attempted to add error handling that would create a new series if this issue occurs, however when the error occurs the code ignores the error handler. Below is the code that I am using. Any feedback on how to address would be greatly appreciated. Thanks.

Code:
Sub FilterRegions()


Dim chtGood As String
Dim chtError As String
Dim chtLabels As String


    With ActiveSheet.Shapes("Drop Down 1")   'amend name to whatever is correct
        
        
        chtGood = Sheets("Summary").Range("C83").Text
        chtError = Sheets("Summary").Range("C84").Text
        chtLabels = Sheets("Summary").Range("C85").Text
        
        ActiveSheet.Range("B38").Value = .ControlFormat.List(.ControlFormat.ListIndex)
        ActiveSheet.Range("$A$41:$T$80").AutoFilter Field:=1, Criteria1:=ActiveSheet.Range("B38").Text
        
        ActiveSheet.ChartObjects("Chart 15").Activate
        
        On Error GoTo ErrorTrap:
        
ErrorTrap:
        If Err.Number <> 0 Then
            ActiveChart.SeriesCollection.NewSeries
            ActiveChart.SeriesCollection.NewSeries
            Resume
        End If
        
        ActiveChart.SeriesCollection(1).Values = chtGood
        ActiveChart.SeriesCollection(1).XValues = chtLabels
        ActiveChart.SeriesCollection(1).Name = "Good Response Stores"


        With ActiveChart.SeriesCollection(1).Format.Fill
            .Visible = msoTrue
            .ForeColor.RGB = RGB(0, 176, 80)
            .Transparency = 0
            .Solid
        End With
        
        ActiveChart.SeriesCollection(2).Values = chtError
        ActiveChart.SeriesCollection(2).XValues = chtLabels
        ActiveChart.SeriesCollection(2).Name = "Incomplete or No Response Stores"
        
         
        With ActiveChart.SeriesCollection(2).Format.Fill
            .Visible = msoTrue
            .ForeColor.RGB = RGB(255, 255, 0)
            .Transparency = 0
            .Solid
        End With
        
    End With


End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Solved... Updated code below

Code:
Sub FilterRegions()


Dim chtGood As String
Dim chtError As String
Dim chtLabels As String


    With ActiveSheet.Shapes("Drop Down 1")   'amend name to whatever is correct
        
        
        chtGood = Sheets("Summary").Range("C83").Text
        chtError = Sheets("Summary").Range("C84").Text
        chtLabels = Sheets("Summary").Range("C85").Text
        
        ActiveSheet.Range("B38").Value = .ControlFormat.List(.ControlFormat.ListIndex)
        ActiveSheet.Range("$A$41:$T$80").AutoFilter Field:=1, Criteria1:=ActiveSheet.Range("B38").Text
        
        ActiveSheet.ChartObjects("Chart 15").Activate
        
ErrorTrap:
        If Err.Number <> 0 Then
            ActiveChart.SeriesCollection.NewSeries
            ActiveChart.SeriesCollection.NewSeries
            Resume
        End If
        
        On Error GoTo ErrorTrap:
        ActiveChart.SeriesCollection(1).Values = chtGood
        ActiveChart.SeriesCollection(1).XValues = chtLabels
        ActiveChart.SeriesCollection(1).Name = "Good Response Stores"
        ActiveChart.SeriesCollection(1).ApplyDataLabels
        ActiveChart.SeriesCollection(1).DataLabels.Format.TextFrame2.TextRange.Font.Bold = msoTrue


        With ActiveChart.SeriesCollection(1).Format.Fill
            .Visible = msoTrue
            .ForeColor.RGB = RGB(0, 176, 80)
            .Transparency = 0
            .Solid
        End With
        
        ActiveChart.SeriesCollection(2).Values = chtError
        ActiveChart.SeriesCollection(2).XValues = chtLabels
        ActiveChart.SeriesCollection(2).Name = "Incomplete or No Response Stores"
        ActiveChart.SeriesCollection(2).ApplyDataLabels
        ActiveChart.SeriesCollection(2).DataLabels.Format.TextFrame2.TextRange.Font.Bold = msoTrue
        
         
        With ActiveChart.SeriesCollection(2).Format.Fill
            .Visible = msoTrue
            .ForeColor.RGB = RGB(255, 255, 0)
            .Transparency = 0
            .Solid
        End With
        
    End With


End Sub
 
Upvote 0
If I understand correctly, instead of error trapping, you could add data series until you have at least two.

Code:
[color=darkblue]Do[/color] [color=darkblue]Until[/color] ActiveChart.SeriesCollection.Count >= 2
    ActiveChart.SeriesCollection.NewSeries
[color=darkblue]Loop[/color]
 
Upvote 0
That was simple and elegant fix!!! I didn't event think of creating a loop to resolve the issue.
 
Upvote 0

Forum statistics

Threads
1,214,407
Messages
6,119,332
Members
448,888
Latest member
Arle8907

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