数据透视图宏记录-导致运行时错误的图表名称

问题描述 投票:0回答:1

记录透视图创建的宏。创建数据透视表和图表,然后编辑图表的属性。

代码运行良好,直到尝试编辑图表的属性为止。这时我收到此消息:

“运行时错误'2147024809(80070057)'

找不到具有指定名称的项目

似乎是图表命名问题。示例:当我记录宏时,Excel将图表命名为“图表1”。但是,当我重新创建场景时,该图表随后被称为“图表2”,因此该宏不起作用。如果我手动将名称更改为“图表2”,则代码执行正常。

如果在记录宏时尝试命名图表,则会遇到类似的问题。也就是说,它尝试将“图表1”更改为“新图表名称”,但找不到“图表1”。

对VBA不太有经验,但是我认为可以通过某种方式来命名图表。我尝试添加一个简单的...

ActiveChart.Name = "Name of this Chart"

...创建图表之后,但是没有用。

下面的完整代码。非常感谢您的帮助!

'
' Create_NITS_Pivot Macro
'

'
    Columns("A:AE").Select
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "'Tags Data Dump'!R1C1:R200C31", Version:=6).CreatePivotTable _
        TableDestination:="'NITS Pivot Chart'!R1C1", TableName:="PivotTable15", _
        DefaultVersion:=6
    Sheets("NITS Pivot Chart").Select
    Cells(1, 1).Select
    With ActiveSheet.PivotTables("PivotTable15")
        .ColumnGrand = True
        .HasAutoFormat = True
        .DisplayErrorString = False
        .DisplayNullString = True
        .EnableDrilldown = True
        .ErrorString = ""
        .MergeLabels = False
        .NullString = ""
        .PageFieldOrder = 2
        .PageFieldWrapCount = 0
        .PreserveFormatting = True
        .RowGrand = True
        .SaveData = True
        .PrintTitles = False
        .RepeatItemsOnEachPrintedPage = True
        .TotalsAnnotation = False
        .CompactRowIndent = 1
        .InGridDropZones = False
        .DisplayFieldCaptions = True
        .DisplayMemberPropertyTooltips = False
        .DisplayContextTooltips = True
        .ShowDrillIndicators = True
        .PrintDrillIndicators = False
        .AllowMultipleFilters = False
        .SortUsingCustomLists = True
        .FieldListSortAscending = False
        .ShowValuesRow = False
        .CalculatedMembersInFilters = False
        .RowAxisLayout xlCompactRow
    End With
    With ActiveSheet.PivotTables("PivotTable15").PivotCache
        .RefreshOnFileOpen = False
        .MissingItemsLimit = xlMissingItemsDefault
    End With
    ActiveSheet.PivotTables("PivotTable15").RepeatAllLabels xlRepeatLabels
    ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
    ActiveChart.SetSourceData Source:=Range("'NITS Pivot Chart'!$A$1:$C$18")
    ActiveChart.PivotLayout.PivotTable.AddDataField ActiveChart.PivotLayout. _
        PivotTable.PivotFields("NITS 16"), "Sum of NITS 16", xlSum
    ActiveChart.PivotLayout.PivotTable.AddDataField ActiveChart.PivotLayout. _
        PivotTable.PivotFields("NITS 17"), "Sum of NITS 17", xlSum
    ActiveChart.PivotLayout.PivotTable.AddDataField ActiveChart.PivotLayout. _
        PivotTable.PivotFields("NITS 18"), "Sum of NITS 18", xlSum
    ActiveChart.Name = "NITS_Chart"
    With ActiveChart.PivotLayout.PivotTable.DataPivotField
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveChart.PivotLayout.PivotTable.PivotFields("Utility Acct/Cust#")
        .Orientation = xlPageField
        .Position = 1
    End With
    With ActiveChart.PivotLayout.PivotTable.PivotFields("Sub-account name")
        .Orientation = xlColumnField
        .Position = 1
    End With
    ActiveSheet.Shapes("Chart 2").IncrementLeft -1030.5
    ActiveSheet.Shapes("Chart 2").IncrementTop -192.5
End Sub

excel vba runtime-error excel-charts
1个回答
0
投票

这里最好的方法是养成从AddChart之类的方法捕获返回值的习惯:

Dim co As Shape
Set co = ActiveSheet.Shapes.AddChart()

这里co是一个Shape / ChartObject,您可以稍后在代码中引用它,而不是用.Select标记所有内容并使用ActiveThing希望以后再使用它。

所以:

co.IncrementLeft -1030.5

或引用所包含的Chart

co.Chart.SetSourceData

etc etc。

创建枢纽缓存和可枢纽对象的方法相同。

© www.soinside.com 2019 - 2024. All rights reserved.