【问题标题】:Excel VBA delete pivot chart with macroExcel VBA删除带有宏的数据透视图
【发布时间】:2014-06-10 15:48:42
【问题描述】:

我的 Excel 工作表上有一个按钮,单击该按钮可创建数据透视表和数据透视图。由于它将运行多次,因此每次运行时我都需要删除现有的表格和图表。表格没问题,但图表每次都会用递增的数字重命名。我需要知道运行时图表的名称才能将其删除。

这是我创建的表格/图表:

   Sheets("EditListingCriteria").Select
    RowNumber = "R" & Application.CountA(Range("A:A")) & "C19"

    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveWorkbook.Worksheets("PivotTable").PivotTables("PivotTable5").PivotCache. _
        CreatePivotTable TableDestination:="Charts!R5C3", TableName:="PivotTable6" _
        , DefaultVersion:=xlPivotTableVersion14
    Sheets("Charts").Select
    Cells(5, 3).Select
    ActiveSheet.PivotTables("PivotTable6").AddDataField ActiveSheet.PivotTables( _
        "PivotTable6").PivotFields("Material Weight"), "Sum of Material Weight", xlSum
    With ActiveSheet.PivotTables("PivotTable6").PivotFields("Shipment Month")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable6").PivotFields("Ordered Ship Mode")
        .Orientation = xlPageField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable6").PivotFields("Actual Ship Mode")
        .Orientation = xlColumnField
        .Position = 1
    End With
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xl3DColumnClustered
    ActiveChart.SetSourceData Source:=Range("Charts!$C$5:$F$12")
    'Rename chart here?
    Rows("2:12").RowHeight = 0
    Rows("1:1").RowHeight = 0

还有删除:

ActiveSheet.ChartObjects("Chart 10").Activate  
ActiveChart.Parent.delete

我尝试了以下重命名,但它总是中断

 chartname = ActiveChart.Name
ActiveSheet.Shapes(chartname).Name = "MonthlyChart"

【问题讨论】:

    标签: vba excel pivot


    【解决方案1】:

    试试这个:

    'To create a chart
    Dim S As Worksheet
    Dim C As Chart
    
    Set S = Activesheet
    Set C = S.Shapes.AddChart.Chart
    S.Shapes(C.Parent.Name).Name = "MonthlyChart"
    

    'To delete the chart later in the same sub:
    C.Parent.Delete
    
    'Or if your delete code is in a different sub (I suspect it is):
    Dim S As Worksheet
    Set S = Activesheet
    
    S.ChartObjects("MonthlyChart").Delete
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多