[Excel VBA] How to Delete All Charts on a Specific Sheet (Using ChartObjects)

目次

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 ChartObjects to 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 While Loop: 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

  1. Target Object: ChartObjects applies only to embedded charts (charts placed inside a standard worksheet). It does not apply to standalone “Chart Sheets”.
  2. Sheet Name: Please change the sheet name (set to “Dashboard” in the code) to match your actual environment.
  3. 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).Delete into 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.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次