来自VBA宏的数据透视表显示不正确的数据

时间:2017-08-01 07:33:02

标签: excel vba excel-vba pivot-table

不知何故,从我的VBA脚本创建的数据透视表显示的数据与手动创建数据透视表时的数据不同。

我的宏执行以下操作:

  • 数据库位于工作表2中。数据透视表位于工作表1中。
  • 宏删除工作表2中的所有行并添加新数据。
  • M删除包含当前数据透视的行。
  • M删除数据透视的数据连接。
  • M为表2中的新数据创建新的数据连接并创建新数据 在表1中转动。

一切正常,但不知何故,数据与我手动创建数据时的数据不同。

我的猜测是它使用了一些旧的数据连接,但我不确定如何或如何解决这个问题。

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

提前致谢

编辑:

只是为了澄清数据是如何不正确的:

  • 一行的独特计数给我提供了1000
  • 我运行宏,并创建了带有修改数据的新数据透视
  • 不同的计数仍显示1000
  • 手动创建的数据透视表,例如980表示不同的计数

0 个答案:

没有答案
相关问题