目次
Background
When creating multiple charts on an Excel sheet, you may encounter situations where you want to delete them all at once.
In my case, I had a sheet serving as a “Dashboard” with many charts placed on it. Since deleting every chart manually was tedious, I created a VBA script to delete them all in one batch.
Goal
- Instantly delete all charts on the sheet named “Dashboard”.
- Use
ChartObjectsto check for the existence of charts and delete them in a loop.
Implementation Code (VBA)
Sub DeleteAllChartsOnSheet()
Dim dashboardSheet As Worksheet
' Set the target sheet (Change "Dashboard" to your sheet name)
Set dashboardSheet = ThisWorkbook.Sheets("Dashboard")
' Delete all existing charts on the dashboard
Do While dashboardSheet.ChartObjects.Count > 0
dashboardSheet.ChartObjects(1).Delete
Loop
End Sub
Code Explanation
ChartObjects.Count: Retrieves the number of charts currently on the sheet..ChartObjects(1).Delete: Deletes the first chart found in the collection.Do WhileLoop: Repeats the deletion process as long as the chart count is greater than 0.
With this structure, all charts are guaranteed to be deleted, regardless of how many there are.
Important Notes
- Target Object:
ChartObjectsapplies only to embedded charts (charts placed inside a standard worksheet). It does not apply to standalone “Chart Sheets”. - Sheet Name: Please change the sheet name (set to “Dashboard” in the code) to match your actual environment.
- Backup: Since this operation deletes all charts on the specified sheet permanently, I strongly recommend saving a backup before running the script.
Summary
- You can delete all charts on a specific sheet at once using VBA.
- This is achieved by incorporating
ChartObjects(1).Deleteinto a loop. - This is a highly useful maintenance tool for environments where graphs are frequently updated or recreated.
If you need to clear a large number of charts instantly, please give this a try.
