|
楼主 |
发表于 2018-7-26 15:00
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub 生成数据透视表()
Dim strSourceData As String, iR As Integer, iC As Integer
iR = Sheets("基础表").Range("S65536").End(xlUp).Row
iC = Sheets("基础表").Range("Y1").End(xlToLeft).Column
strSourceData = "基础表!R6C3:R&iR&C& iC"
Sheets("数据透视表").Rows.Clear
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=strSourceData, Version:=xlPivotTableVersion10).CreatePivotTable(TableDestination:="数据透视表!R1C1", TableName:="透视表", DefaultVersion:=xlPivotTableVersion10)
Sheets("数据透视表").Activate
With ActiveSheet.PivotTables("数据透视表").PivotFields("本期增加(数量)")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("数据透视表").PivotFields("编码")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("数据透视表").PivotFields("产品")
.Orientation = xlRowField
.Position = 2
End With
ActiveSheet.PivotTables("数据透视表").RowAxisLayout xlTabularRow
ActiveSheet.PivotTables("数据透视表").AddDataField ActiveSheet.PivotTables("数据透视表" _
).PivotFields("本期增加(数量)"), "求和:本期增加(数量)", xlSum
End Sub |
|