VBA实战——创建数据透视表
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Sub 创建数据透视表()
s.Add Name:="database1", RefersToR1C1:= _
"=OFFSET(R4C3,,,COUNTA(C3),COUNTA(R4))"
s("database1").Comment = ""
Rows("5:5").Select
Selection.Delete Shift:=xlUp
Range("C4").Select
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"database1", Version:=xlPivotTableVersion14).CreatePivotTable _ TableDestination:="Sheet1!R3C1", TableName:="数据透视表1", DefaultVersion:= _ xlPivotTableVersion14
Sheets("Sheet1").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("数据透视表1").PivotFields("指令单单号")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("数据透视表1").PivotFields("名称1")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("数据透视表1").PivotFields("作业")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("数据透视表1").PivotFields("物料")
.Orientation = xlRowField
.Position = 3
End With
With ActiveSheet.PivotTables("数据透视表1").PivotFields("物料描述")
.Orientation = xlRowField
.Position = 4
End With
With ActiveSheet.PivotTables("数据透视表1").PivotFields("供应商")
.Orientation = xlRowField
.Position = 5
End With
ActiveSheet.PivotTables("数据透视表1").AddDataFieldActiveSheet.PivotTables("数据透视表1" _ ).PivotFields("需求量"), "求和项:需求量", xlSum
Range("B21").Select
With ActiveSheet.PivotTables("数据透视表1")
.InGridDropZones = True
.RowAxisLayoutxlTabularRow
End With
Range("D6").Select
ActiveSheet.PivotTables("数据透视表1").PivotFields("物料描述").Subtotals = Array(False, _ False, False, False, False, False, False, False, False, False, False, False)
Range("C6").Select
ActiveSheet.PivotTables("数据透视表1").PivotFields("物料").Subtotals = Array(False, _ False, False, False, False, False, False, False, False, False, False, False)
Range("B6").Select
ActiveSheet.PivotTables("数据透视表1").PivotFields("作业").Subtotals = Array(False, _ False, False, False, False, False, False, False, False, False, False, False)
Range("A7").Select
ActiveSheet.PivotTables("数据透视表1").PivotFields("名称1").Subtotals = Array(False, _ False, False, False, False, False, False, False, False, False, False, False) ActiveSheet.PivotTables("数据透视表1").PivotSelect "", xlDataAndLabel, True Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0