• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Chart updating problem

Villalobos

Active Member
Hello,

I would like to ask some help from you regarding the chart updating.

I use this code to create chart about the data source:
Code:
Sub WeeklyChart()
Dim SourceSheet1 As Worksheet
Dim SourceLastrow1 As Long
Dim cht2 As Chart
  Set SourceSheet1 = Worksheets("Evaluation")
  With SourceSheet1
  SourceLastrow1 = .Cells(.Rows.Count, "T").End(xlUp).Row
  End With
  
  Charts.Add
  ActiveChart.ChartType = xlColumnClustered
  ActiveChart.SeriesCollection.NewSeries
  ActiveChart.HasLegend = False
  ActiveChart.HasTitle = False
  ActiveChart.SeriesCollection(1).XValues = "=Evaluation!R9C21:R" & SourceLastrow1 & "C21"
  ActiveChart.SeriesCollection(1).Values = "=Evaluation!R9C23:R" & SourceLastrow1 & "C23"
  ActiveChart.SeriesCollection(1).Interior.Color = RGB(204, 204, 255)
  ActiveChart.SeriesCollection(1).HasDataLabels = True
  ActiveChart.ChartArea.Border.LineStyle = False
  ActiveChart.Location Where:=xlLocationAsObject, Name:="Evaluation"
  
  With ActiveChart.Parent
  .Left = 935
  .Width = 650
  .Top = 505
  .Height = 225
  End With
  Set cht2 = Sheets("Evaluation").ChartObjects("Diagram 1").Chart
  With cht2.Axes(xlCategory, xlPrimary)
  .HasMajorGridlines = False
  .HasMinorGridlines = False
  End With
 
  With cht2.Axes(xlValue, xlPrimary)
  .HasMajorGridlines = False
  .HasMinorGridlines = False
  End With
  
  With ActiveChart.SeriesCollection.NewSeries
  .ChartType = xlLine
  .Interior.Color = RGB(255, 0, 0)
  .XValues = "=Evaluation!R9C21:R" & SourceLastrow1 & "C21"
  .Values = "=Evaluation!R9C24:R" & SourceLastrow1 & "C24"
  End With
End Sub

At the first run the code is working fine, just if I add new data to the source then the code add new series to chart (and change the colors also)... I do not why...

By the way if I run again the code (if data source not changed) the code give back the expected chart result. o_O

The target would be that if the data source has been changed then (after the code run) the chart changing as well (without any bug).

Do you have some idea how could be avoid this kind of problem?



Thanks in advance the reply!
 

Attachments

  • Sample.xlsm
    30 KB · Views: 1
Hi Narayan,

Tha code want to give too many series to the chart (I do not know why), but if I just delete the unnecessary series then I receive the expected result. The problem solved.

Code:
With ActiveChart
  Do Until .SeriesCollection.Count = 0
  .SeriesCollection(1).Delete
  Loop
  End With

Thank you for your attention!
 
Back
Top