[英]vba to create a pivot table excel 2015
I wanted to create a Pivot table in Excel 2015. 我想在Excel 2015中创建数据透视表。
I recorded the macros to create my own vba code. 我记录了宏以创建自己的vba代码。 But I couldn't make it. 但是我做不到。 Also, I referred the links available here, but I couldn't make it out from them because they followed other way, which was hard for me. 另外,我引用了这里可用的链接,但是我无法从它们中识别出来,因为它们遵循了其他方式,这对我来说很难。
Could anyone help me to frame the VBA code for the below recorded macros and explain in the comment the steps? 谁能帮助我为下面记录的宏构建VBA代码,并在注释中说明步骤? This would be really helpful for me as I am generating the Pivot table in vba for first time. 当我第一次在vba中生成数据透视表时,这对我真的很有帮助。
Sub Macro4()
'
' Macro4 Macro
'
'
Cells.Select
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Preparation sheet!R1C1:R1048576C8", Version:=xlPivotTableVersion15). _
CreatePivotTable TableDestination:="Sheet21!R3C1", TableName:="PivotTable7" _
, DefaultVersion:=xlPivotTableVersion15
Sheets("Sheet21").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable7").PivotFields("Category")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable7").PivotFields("Colour")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable7").PivotFields("Category")
.PivotItems("DG-035583").Visible = False
.PivotItems("DG-048917").Visible = False
.PivotItems("DG-Series").Visible = False
.PivotItems("gn").Visible = False
.PivotItems("yl").Visible = False
.PivotItems("(blank)").Visible = False
End With
With ActiveSheet.PivotTables("PivotTable7").PivotFields("Colour")
.PivotItems("(blank)").Visible = False
End With
End Sub
Try the code below to create/update a PivotTable
, explanations inside the code as comments: 尝试下面的代码创建/更新PivotTable
,其中的解释为注释:
Option Explicit
Sub AutoPivot()
Dim PvtCache As PivotCache
Dim PvtTbl As PivotTable
Dim PvtSht As Worksheet
' set Pivot Cache for Pivot Table
' Your range is static, there are ways to refer to a dynamic range
Set PvtCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="Preparation sheet!R1C1:R1048576C8")
' set the Pivot table's sheet
Set PvtSht = Worksheets("Sheet21")
' add this line in case the Pivot table doesn't exit >> first time running this Macro
On Error Resume Next
Set PvtTbl = PvtSht.PivotTables("PivotTable7") ' check if "PivotTable7" Pivot Table already created (in past runs of this Macro)
On Error GoTo 0
If PvtTbl Is Nothing Then ' Pivot table object is nothing >> create it
' create a new Pivot Table in "PivotTable4" sheet
Set PvtTbl = PvtSht.PivotTables.Add(PivotCache:=PvtCache, TableDestination:=PvtSht.Range("A3"), TableName:="PivotTable7")
With PvtTbl
With .PivotFields("Category")
.Orientation = xlRowField
.Position = 1
End With
With .PivotFields("Colour")
.Orientation = xlColumnField
.Position = 1
End With
With .PivotFields("Category")
.PivotItems("DG-035583").Visible = False
.PivotItems("DG-048917").Visible = False
.PivotItems("DG-Series").Visible = False
.PivotItems("gn").Visible = False
.PivotItems("yl").Visible = False
.PivotItems("(blank)").Visible = False
End With
With .PivotFields("Colour")
.PivotItems("(blank)").Visible = False
End With
End With
Else
' just refresh the Pivot cache with the updated Range
PvtTbl.ChangePivotCache PvtCache
PvtTbl.RefreshTable
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.