2017-06-19 219 views
-1

我想在Excel 2015中创建数据透视表。vba创建数据透视表excel 2015

我记录了宏以创建自己的vba代码。但我无法做到。另外,我提到了这里提供的链接,但是我无法从这些链接中找到它们,因为它们采用了其他方式,这对我来说很难。

任何人都可以帮助我为以下录制的宏构造VBA代码,并在注释中解释步骤?这对我来说真的很有帮助,因为我第一次生成vba中的Pivot表。

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 
+1

你至少应该指出的是** **正是你的问题。 “*创建一个数据透视表*”并不能很好地描述你试图达到的目标。你需要用**详细的**信息来询问**特定的**问题你的代码中出了什么问题。请阅读[问]和[为什么“有人可以帮助我?”不是真正的问题?](https://meta.stackoverflow.com/a/284237/3219613)以改善您的问题。 –

回答

2

尝试下面的代码创建/更新的代码里面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 
+0

我将在使用代码后更新您。 – Mikz

+0

@Mikz是什么?随意问 –

+0

我用上面的代码并执行它。我得到一个错误应用程序定义或对象定义的错误。我如何克服这一点? – Mikz

相关问题