Deleting charts in excel using vba

33,073

This should take care of your needs. It prompts the user also, via MsgBox as a precaution against deleting wanted charts/objects.

Sub delAllChartsInWorkbook()
'
' delchart Macro
' Macro recorded 1/4/2016 by ejbytes
'
    'Variables
    Dim count As Integer
    Dim LIST As Integer
    Dim currSheet As Worksheet
    Dim myChart As ChartObject
    Dim YesOrNoAnswerToMessageBox As String
    Dim QuestionToMessageBox As String
    Dim tmpName As String


    'Define Variables
    count = ActiveWorkbook.Worksheets.count

    '****************************************************
    'Loop example using a count.                        *
    '****************************************************
    For LIST = 1 To count
        MsgBox "This is a loop example. This is Sheet: " _
                 & ActiveWorkbook.Worksheets(LIST).Name
    Next LIST



    '****************************************************
    '* Loop example all sheet in this workbook          *
    '* Cycle through each Sheet                         *
    '* Nested Loops:                                    *
    '*                                                  *
    '* Outer Loop                                       *
    '****************************************************
    For Each currSheet In Worksheets
        MsgBox "Current sheet: " & currSheet.Name

        '****************************************************
        '* Cycle through each Chart Object on current sheet *
        '* Sheets("Sheet1").ChartObjects                    *
        '*                                                  *
        '* Inner Loop                                       *
        '****************************************************
        For Each myChart In currSheet.ChartObjects

            QuestionToMessageBox = "DELETE chart: '" & myChart.Name & "' ?"
            YesOrNoAnswerToMessageBox = _
                MsgBox(QuestionToMessageBox, vbYesNo, "Yes/No Comment?")

            If YesOrNoAnswerToMessageBox = vbNo Then
                MsgBox "Chart: " & myChart.Name & " skipped."
            Else
                tmpName = myChart.Name
                myChart.Delete
                MsgBox "Chart: " & tmpName & " Deleted!"
                'Modify my chart? You can do this too:
                'myChart.Chart.ChartType = xlLine
            End If
        Next myChart
    Next

End Sub
Share:
33,073

Related videos on Youtube

Ashwith Ullal
Author by

Ashwith Ullal

Updated on September 18, 2022

Comments

  • Ashwith Ullal
    Ashwith Ullal over 1 year

    How to delete all charts in Excel Workbooks using VBA.

    ActiveSheet.ChartObjects("Chart 3").Activate
    ActiveChart.Parent.Delete
    
  • Norbert Metternich
    Norbert Metternich over 3 years
    Plagiarized solution.
  • Excellll
    Excellll over 3 years
    Can you please provide attribution instead of saying in a comment that your work is plagiarized? Edit the credit to the author into the answer, please.