Office版本2016的数据透视表错误

时间:2019-05-09 18:28:49

标签: excel vba pivot-table

我的Office版本是3652019。在Office 2016中运行相同的代码时,它带来的收益是季度而不是月份,因此我遇到了错误。我认为可能是在宿舍。

With ActiveSheet.PivotTables("ResumenRuido").PivotCache
     ActiveSheet.PivotTables("ResumenRuido").PivotSelect "", xlDataAndLabel, True
    ActiveSheet.PivotTables("ResumenRuido").NullString = "0"
    .DisplayNullString = True
    .RefreshOnFileOpen = False
    .MissingItemsLimit = xlMissingItemsDefault
End With

With ActiveSheet.PivotTables("ResumenRuido").PivotFields("Codigo SIC")
    .DisplayNullString = True
    .Orientation = xlRowField
    .Position = 1
End With
With ActiveSheet.PivotTables("ResumenRuido").PivotFields("Fecha")
    .DisplayNullString = True
    .Orientation = xlColumnField
    .Position = 1
End With

ActiveSheet.PivotTables("ResumenRuido").PivotFields("Fecha").AutoGroup

ActiveSheet.PivotTables("ResumenRuido").PivotFields("Quarters").PivotItems( _
    "Qtr1").ShowDetail = True

ActiveSheet.PivotTables("ResumenRuido").PivotFields("Years").PivotItems("2019") _
    .ShowDetail = True
ActiveSheet.PivotTables("ResumenRuido").PivotFields("Fecha").Orientation = _
    xlHidden
ActiveSheet.PivotTables("ResumenRuido").PivotFields("Quarters").Orientation = _
    xlHidden
With ActiveSheet.PivotTables("ResumenRuido").PivotFields("Fecha")
    .Orientation = xlColumnField
    .Position = 2
End With

ActiveSheet.PivotTables("ResumenRuido").PivotFields("Years").ShowDetail = True

Range("F4").Select
ActiveSheet.PivotTables("ResumenRuido").RowGrand = False
Range("E4").Select
ActiveSheet.PivotTables("ResumenRuido").PivotFields("Years").Subtotals = Array( _
    False, False, False, False, False, False, False, False, False, False, False, False)
Range("F10").Select

ActiveSheet.PivotTables("ResumenRuido").AddDataField ActiveSheet.PivotTables( _
    "ResumenRuido").PivotFields("Penalidad"), "Sum of Penalidad", xlSum
ActiveSheet.PivotTables("ResumenRuido").RowAxisLayout xlTabularRow
ActiveSheet.Name = "Resumen_penalidades"

Range("E10").Select
ActiveSheet.PivotTables("ResumenRuido").NullString = ""

Call tabla_resumen_energia

End Sub

1 个答案:

答案 0 :(得分:0)

首先,我建议使用对数据透视表的引用来缩短代码。在这两行之后,您可以将每个“ ActiveSheet.PivotTables(“ ResumenRuido”)“替换为“ pt”。

Dim pt As PivotTable
Set pt = ActiveSheet.PivotTables("ResumenRuido")

我建议使用专用的取消分组/分组方法来代替PivotField.AutoGroup,以确保获得所需的期限,例如。 G。年,季度和月份:

With pt.PivotFields("Fecha")
    ' if already grouped, first ungroup it:
    If .TotalLevels > 1 Then .DataRange.Cells(1).Ungroup

    ' sort it:
    .AutoSort Order:=xlAscending, Field:="Fecha"

    ' group it by wanted periods:
    ' seconds, minutes, hours, days, months, quarters, years
    .DataRange.Cells(1).Group _
        Periods:=Array(False, False, False, False, True, True, True)
End With

将PivotField分组的错误

我可以确认,存在一个错误(至少在我的Excel版本“ Office 365 Pro Plus,版本1902,内部版本11328.20100单击并运行”)。如果我为具有日期的枢轴字段手动选择分组,则选择的最低期间始终未显示作为附加的PivotField。
示例:如果我手动或与我一起分组年/季度/月。米代码,不显示月份!


解决方法

具有更短时间段的组(在某些Excel版本中未显示),如果显示,以后也将其隐藏:

    .DataRange.Cells(1).Group _
        Periods:=Array(False, False, False, True, True, True, True)

' ...

Dim pf As PivotField
For Each pf In pt.PivotFields
    Select Case pf.Name
    Case "Fecha", "Quarters", "Months", "Days"
        pf.Orientation = xlHidden
    End Select
Next pf