2016-07-07 44 views
1

我已经在VBA中创建了一个包含字符串数据的数据透视表,但似乎无法折叠数据透视表中的所有字段,我该怎么做? 这里是我的源代码VBA数据透视表折叠所有字段

SrcData = ActiveSheet.Name & "!" & Range(Cells(1, 1), Cells(46, 3)).Address(ReferenceStyle:=xlR1C1) 
StartPvt = Sheets("Key Controls").Cells(2, 5).Address(ReferenceStyle:=xlR1C1) 
Set pvtCache = ActiveWorkbook.PivotCaches.Create(_ 
    SourceType:=xlDatabase, _ 
    SourceData:=SrcData) 
Set pvt = pvtCache.CreatePivotTable(_ 
    TableDestination:=StartPvt, _ 
    TableName:="PivotTable1") 
pvt.PivotFields("SOP Reference").Orientation = xlRowField 
pvt.PivotFields("Key Control ID").Orientation = xlRowField 
pvt.PivotFields("Key Control Name").Orientation = xlRowField 
+1

行字段的方向应是持仓量也。防爆。 'With ActiveSheet.PivotTables(“PivotTable3”)。PivotFields(“Key Control ID”) .Orientation = xlRowField .Position = 2 End With'假设你想将整个字段折叠为“SOP Reference”选择一个合适的单元格say 'A15'code将为'Range(“A15”)。选择 ActiveSheet.PivotTables(“PivotTable3”)。PivotFields(“Key Control Name”)。 _ PivotItems(“electron”)。DrillTo“SOP Reference”'您的表格[看起来像](https://www.dropbox.com/s/51znhaab1z0lnno/pivot_q070716.xlsm?dl=0) – skkakkar

回答

0

使用pf.ShowDetail = False

这是效率的噩梦,你会被卡住了很长一段时间,并有可能会崩溃Excel中!


的好方法采用的是DrillTo

Public Sub PivotTable_Collapse() 

    Dim pT As PivotTable 
    Dim pF As PivotField 

    Set pT = ActiveSheet.PivotTables(1) 

    With pT 
     For Each pF In pT.RowFields 
      pF.DrillTo pF.Name 
     Next pF 
    End With 

End Sub