如何修复不扩展细节的ShowDetail = True

问题描述 投票:1回答:1

以下代码运行良好,因为没有产生运行时错误,并且使用正确的数据生成数据透视表并将其放置在正确的位置。

但是,项目列表已折叠。

部分代码包括:

  • 从列中读取列头
  • 识别可以在一个范围内随机放置的列头
  • 对于每列,构建一维(即单个字段)数据透视表
  • 将枢轴表放在给定的位置
  • 显示所有项目+他们的计数

A)使用SHOWDETAIL = True

B)录制宏并“克隆”代码

For Each headerCell In wSh.Range("Ctl_Headers")
    Set sceRange = wSh.Range(headerCell.Value)
    sceRange.Activate
    fRow = sceRange.Row
    lRow = sceRange.Cells.SpecialCells(xlCellTypeLastCell).Row
    fCol = sceRange.Column
    lCol = fCol - 1 + sceRange.Columns.Count

    Set pivCache = wBk.PivotCaches.Add(xlDatabase, wSh.Range(wSh.Cells(fRow, fCol), wSh.Cells(lRow, lCol)))

    Set pivTable = wSh.PivotTables.Add(PivotCache:=pivCache, TableDestination:=wSh.Range(wSh.Cells(1, nrindex), wSh.Cells(1, nrindex)), TableName:="piv" & headerCell.Value)

    pivTable.PivotFields(headerCell.Value).Orientation = xlRowField
    pivTable.AddDataField pivTable.PivotFields(headerCell.Value), "Count " & headerCell.Value, xlCount
    pivTable.PivotFields(headerCell.Value).ShowDetail = True

    nrindex = nrindex + 4
Next headerCell
excel vba pivot-table xls
1个回答
0
投票

你使用pivTable.PivotFields(headerCell.Value)作为RowField,它的计数为DataField。 您必须更改以下代码行以解决行域问题。

之前:

pivTable.PivotFields(headerCell.Value).ShowDetail = True

后:

pivTable.RowFields(headerCell.Value).ShowDetail = True
© www.soinside.com 2019 - 2024. All rights reserved.