以下代码运行良好,因为没有产生运行时错误,并且使用正确的数据生成数据透视表并将其放置在正确的位置。
但是,项目列表已折叠。
部分代码包括:
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
你使用pivTable.PivotFields(headerCell.Value)
作为RowField
,它的计数为DataField
。
您必须更改以下代码行以解决行域问题。
之前:
pivTable.PivotFields(headerCell.Value).ShowDetail = True
后:
pivTable.RowFields(headerCell.Value).ShowDetail = True