钻取指标VBA数据透视表

时间:2016-07-20 14:41:12

标签: vba excel-vba pivot drilldown excel

我目前正在excel 2010中使用VBA,并创建了一个我希望根据某些内容进行格式化的数据透视表。我试图使用

从表中删除+/-按钮
ActiveSheet.PivotTables("HoldersPivotTable").ShowDrillIndicators = False

但由于某种原因,这不起作用。

非常感谢帮助!

Sub PivotTable_Creation_And_Formatting()

    'PURPOSE: Creates a brand new Pivot table on a new worksheet from data in the ActiveSheet

    Sheets("Sheet2").Select

    Dim sht As Worksheet
    Dim pvtCache As PivotCache
    Dim pvt As PivotTable
    Dim pf As PivotField
    Dim StartPvt As String
    Dim SrcData As String
    Dim LastRow As Long

    LastRow = Cells(Rows.Count, "A").End(xlUp).Row

    'Determine the data range you want to pivot
    SrcData = ActiveSheet.Name & "!" & Range(Cells(1, "A"), Cells(LastRow, "E")).Address

    'Create a new worksheet
    Set sht = Sheets("HOLDERS (CORP)")

    'Where do you want Pivot Table to start?

    StartPvt = sht.Range("A1").Address

    'Create Pivot Cache from Source Data
    Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
    SourceType:=xlDatabase, _
    SourceData:=SrcData)

    'Create Pivot table from Pivot Cache
    Set pvt = pvtCache.CreatePivotTable(TableDestination:=sht.Range(StartPvt), _
    TableName:="HoldersPivotTable")

    pvt.PivotFields("SECURITY SHORT DESCRIPTION").Orientation = xlRowField

    'Add item to the Row Labels
    pvt.PivotFields("HOLDERS").Orientation = xlRowField

    'Add item to the Row Labels
    pvt.PivotFields("POSITION").Orientation = xlRowField

    'Add item to the Row Labels
    pvt.PivotFields("LATEST CHANGE").Orientation = xlRowField

    'Add item to the Row Labels
    pvt.PivotFields("FILE DT").Orientation = xlRowField

    pvt.ColumnGrand = False
    pvt.RowGrand = False

    'Show in Tabular Form
    pvt.RowAxisLayout xlTabularRow

    For Each pf In pvt.PivotFields
        pf.Subtotals(1) = True
        pf.Subtotals(1) = False
    Next pf

    Sheets("HOLDERS (CORP)").Select
    Columns("A:E").Select
    Selection.Columns.AutoFit
    Columns("C:C").ColumnWidth = 13.43
    Columns("A:E").HorizontalAlignment = xlLeft

    Columns("B:E").Interior.Color = RGB(141, 180, 226)
    Range("A1").Interior.Color = RGB(141, 180, 226)

    With Range("A1:E1").Borders
        .LineStyle = xlContinuous
        .Weight = xlMedium
    End With

    ActiveSheet.PivotTables("HoldersPivotTable").ShowDrillIndicators = False

    Range("B1").Select

End Sub

1 个答案:

答案 0 :(得分:0)

将代码更改为:

ActiveSheet.PivotTables("HoldersPivotTable").ShowDrillIndicators = False
ActiveSheet.PivotTables("HoldersPivotTable").PivotCache.Refresh

这将在没有向下钻取指示器的情况下重新显示枢轴。您必须刷新数据透视表才能显示更改。


如果仍然无效,请检查

Application.ScreenUpdating = False

没有相应的

Application.ScreenUpdating = True