Excluindo gráficos no excel usando vba

0

Como excluir todos os gráficos em pastas de trabalho do Excel usando o VBA.

ActiveSheet.ChartObjects("Chart 3").Activate
ActiveChart.Parent.Delete
    
por Ashwith Ullal 05.01.2016 / 06:28

2 respostas

0

Isso deve cuidar de suas necessidades. Ele também solicita ao usuário, via MsgBox, como precaução contra a exclusão de gráficos / objetos desejados.

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
    
por 05.01.2016 / 07:50
0

Para excluir todos os gráficos na planilha ativa:

Sub DeleteChartsOnActiveSheet()
  ActiveSheet.ChartObjects.Delete
End Sub

Para excluir todos os gráficos em todas as planilhas na pasta de trabalho ativa:

Sub DeleteChartsInActiveWorkbook()
  ' delete chart sheets
  ActiveWorkbook.Charts.Delete

  ' delete charts embedded in other sheets
  Dim sh As Object
  For Each sh in ActiveWorkbook.Sheets
    sh.ChartObjects.Delete
  Next
End Sub
    
por 11.01.2016 / 21:46