Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

Excel: How to clear a chart's previous line when error occurs

Experts,

Quick question:  I have a VBA routine that graphs charts when butoon is fired.  However, if nothing is selected to chart and button is fired then I have the title change to "Please select an ID #".  The issues is that I don't know how to get it to clear the previously charted line?  Any ideas to what I can add?
Sub UpdateGenericChartArray(ByVal lbItemCount As Integer, dataRange As String, datasheet As String, chartSheet As String, chartID As String, obUsed As Boolean)
Dim myRng As Variant
Dim Target As String
Dim i As Integer
Dim mySeries As Series
Dim myRange As Range
Dim foundData As Boolean
Dim chrSer As Series

    foundData = False
    
    Sheets(chartSheet).ChartObjects(chartID).Activate
    With ActiveChart
        i = .SeriesCollection.Count - 1
        For Each mySeries In .SeriesCollection
            If i > 0 Then
                mySeries.Delete
                i = i - 1
            End If
        Next mySeries
    End With
    
    For i = 0 To lbItemCount - 1 'for each item in the listbox that was selected
        Target = lbArray(i) 'keep the code as similar as possible to original combobox routine
        On Error Resume Next
        Err.Clear
        
        If Not obUsed Then obMainChart = 4 'go with last 8 weeks as default, to keep all charts working and to chart things that don't have the option button for date range, they will go with last 8 months
        
        myRng = Application.WorksheetFunction.VLookup(Target, Range(dataRange), Range(dataRange).Columns.Count - obMainChart, False)
        
        On Error GoTo 0

        
        If Err.Number = 0 And Not IsEmpty(myRng) Then
            Sheets(chartSheet).ChartObjects(chartID).Activate
            With ActiveChart
                If i = 0 Then 'set x-axis labels only once
                    Set myRange = Range(myRng)
                    .SeriesCollection(1).XValues = "'" & datasheet & "'!" & Range(Cells(5, myRange.Cells(1, 1).Column), Cells(5, myRange.Cells(1, myRange.Columns.Count).Column)).Address
                End If
                    
                If i > 0 Then .SeriesCollection.NewSeries ' don't create the new series till past the first one
                
                .SeriesCollection(i + 1).Values = "'" & datasheet & "'!" & Range(myRng).Address
                .SeriesCollection(i + 1).Name = Left(Target, InStr(Target & " ", " ") - 1) 'add a space after, just in case it doesn't have a first one -> name = initial word before a blank space of the combobox selection
            End With
        foundData = True
        Else
            'do nothing
            foundData = False
        End If
    Next i
    'last step here is to now format the resulting chart
    If foundData Then
        With ActiveChart
            .HasTitle = True
            .ChartTitle.Text = Target
            .Axes(xlValue).Select
            Selection.TickLabels.NumberFormat = Range("'" & datasheet & "'!" & myRange.Cells(1, 1).Address).NumberFormat
                For Each chser In .SeriesCollection
                 chser.Trendlines.Add xlLogarithmic
                
            Next
            
        End With
        SendKeys "{ESC}"
        
        
    Else

        With ActiveChart
            .HasTitle = True
            .ChartTitle.Text = "                  Please select an ID #" & Target
        End With
    End If
End Sub

Open in new window

0
Maliki Hassani
Asked:
Maliki Hassani
  • 4
  • 3
1 Solution
 
Rory ArchibaldCommented:
You should loop backwards when deleting:

    With ActiveChart
        for i = .SeriesCollection.Count to 1 Step -1
        .seriescollection(i).Delete
        Next i
    End With

Open in new window

0
 
Maliki HassaniAuthor Commented:
okay so the problem now is that once the chart is clear completely, I go run another selection and it gives me an error of "invalid Parameter"/  Highlighting

.SeriesCollection(1).XValues = "'" & datasheet & "'!" & Range(Cells(5, myRange.Cells(1, 1).Column), Cells(5, myRange.Cells(1, myRange.Columns.Count).Column)).Address

I attached the spreadsheet.  The code is in the chartingRoutines NOC-Reports-r37.xlsm

To see it fire just unselect all ID's if selected and clcik on "Update Chart"
0
 
Rory ArchibaldCommented:
Use .SeriesCollection.Add before that line so that you actually have a series to manipulate! :)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Maliki HassaniAuthor Commented:
Sorry that isn't working...
0
 
Rory ArchibaldCommented:
Sorry, I meant .SeriesCollection.NewSeries rather than .Add (as you use in the subsequent loop)

Would be easier to amend if I hadn't had to break the VBAProject password to look at the code...
0
 
Maliki HassaniAuthor Commented:
Oh my bad!  Just added that...  You broke it??  lol  
0
 
Rory ArchibaldCommented:
Yup - took a minute or so. ;)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now