不知何故,从我的VBA脚本创建的数据透视表显示的数据与手动创建数据透视表时的数据不同。
我的宏执行以下操作:
一切正常,但不知何故,数据与我手动创建数据时的数据不同。
我的猜测是它使用了一些旧的数据连接,但我不确定如何或如何解决这个问题。
VBA - 透视表创建代码:
'Delete old pivot
Sheets("Sheet1").Select
Range("A1:C5").Select
Selection.ClearContents
'Create new Connection
ActiveWorkbook.Connections("PivotConnection").Delete
Workbooks("File.xlsm").Connections.Add2 _
"PivotConnection", "", _
"WORKSHEET;*ABSOLUTE PATH*[File.xlsm]Sheet2" _
, "Sheet2!$A:$Q", 7, True, False
'Create new pivot
ActiveWorkbook.PivotCaches.Create(SourceType:=xlExternal, SourceData:= _
ActiveWorkbook.Connections("PivotConnection"), Version:=6).CreatePivotTable _
TableDestination:=Worksheets("Sheet1").Cells(1, 1), TableName:="PivotTable3", DefaultVersion:=6
With ActiveSheet.PivotTables("PivotTable3")
.ColumnGrand = True
.HasAutoFormat = True
.DisplayErrorString = False
.DisplayNullString = True
.EnableDrilldown = True
.ErrorString = ""
.MergeLabels = False
.NullString = ""
.PageFieldOrder = 2
.PageFieldWrapCount = 0
.PreserveFormatting = True
.RowGrand = True
.PrintTitles = False
.RepeatItemsOnEachPrintedPage = True
.TotalsAnnotation = True
.CompactRowIndent = 1
.VisualTotals = False
.InGridDropZones = False
.DisplayFieldCaptions = True
.DisplayMemberPropertyTooltips = True
.DisplayContextTooltips = True
.ShowDrillIndicators = True
.PrintDrillIndicators = False
.DisplayEmptyRow = False
.DisplayEmptyColumn = False
.AllowMultipleFilters = False
.SortUsingCustomLists = True
.DisplayImmediateItems = True
.ViewCalculatedMembers = True
.FieldListSortAscending = False
.ShowValuesRow = False
.CalculatedMembersInFilters = True
.RowAxisLayout xlCompactRow
End With
ActiveSheet.PivotTables("PivotTable3").PivotCache.RefreshOnFileOpen = False
ActiveSheet.PivotTables("PivotTable3").RepeatAllLabels xlRepeatLabels
提前致谢
编辑:
只是为了澄清数据是如何不正确的: