2017-06-19 264 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

相關問題