VBA Clear Chart Dynamically and refresh with new values

jammerules

New Member
Joined
Nov 16, 2009
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a chart named "VelChart". It fetches data from a set of dynamically created cell values. However, I want the chart to be cleared of any graph every time I said referenced cell values are refreshed.

1669224644720.png


I have this code that isn't working:

VBA Code:
        Worksheets("Velocity Tracker").ChartObjects("VelChart").Activate
        ActiveChart.ChartArea.ClearContents

When I use the above code, it is deleting the chart altogether. And when I try to reference the chart elsewhere in the code, it throws an error. How do I just clear the chart contents and not delete the chart altogether?


This is the code that I have elsewhere that I am referencing:

VBA Code:
Public Function DrawVelChart(strComponent As String)
    Select Case strComponent
        Case "Infrastructure (CI/CD/DevOps)"
            Worksheets("Velocity Tracker").ChartObjects("VelChart").Activate
            ActiveChart.SetSourceData Source:=Worksheets("Velocity Tracker").Range("C6:E" & iLastRow)
            ActiveChart.ChartTitle.Text = "Infra Velocity Trend"
            ActiveChart.SeriesCollection(1).XValues = Worksheets("Velocity Tracker").Range("G6:G" & iLastRow)
        Case "Configuration"
            Worksheets("Velocity Tracker").ChartObjects("VelChart").Activate
            ActiveChart.SetSourceData Source:=Worksheets("Velocity Tracker").Range("C6:E" & iLastRow)
            ActiveChart.ChartTitle.Text = "Configuration Velocity Trend"
            ActiveChart.SeriesCollection(1).XValues = Worksheets("Velocity Tracker").Range("G6:G" & iLastRow)
        Case "Analytics"
            Worksheets("Velocity Tracker").ChartObjects("VelChart").Activate
            ActiveChart.SetSourceData Source:=Worksheets("Velocity Tracker").Range("C6:E" & iLastRow)
            ActiveChart.ChartTitle.Text = "Analytics Velocity Trend"
            ActiveChart.SeriesCollection(1).XValues = Worksheets("Velocity Tracker").Range("G6:G" & iLastRow)
    End Select
End Function
 
1. Are the sheet name ("Velocity Tracker") and chart name ("VelChart") in the code correct?

2. Why must you clear the chart? Is it because the number of rows in the data range changes? Then stop wasting time with VBA, and convert the data into a Table; don't include any extra rows. Make the chart, using data from entire columns of the Table. When data is added or removed from the Table, the Table changes size to accommodate, and formulas (including the chart's SERIES formulas) also change accordingly. see Easy Dynamic Charts Using Lists or Tables.
 
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Forum statistics

Threads
1,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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