Solved

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

Posted on 2011-03-04
7
254 Views
Last Modified: 2012-05-11
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
Comment
Question by:Maliki Hassani
  • 4
  • 3
7 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35037254
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
 

Author Comment

by:Maliki Hassani
ID: 35037393
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
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35037416
Use .SeriesCollection.Add before that line so that you actually have a series to manipulate! :)
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

Author Comment

by:Maliki Hassani
ID: 35037720
Sorry that isn't working...
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 35037831
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
 

Author Comment

by:Maliki Hassani
ID: 35037949
Oh my bad!  Just added that...  You broke it??  lol  
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35038000
Yup - took a minute or so. ;)
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question