2017-09-13 77 views
0

我記錄了一個宏,我想在其中創建一個數據透視表到新的工作表中。我正在使用2010版本。無法在VBA中的新工作表中創建數據透視表

我有「運行時錯誤5」無效的過程調用或參數」的錯誤,當我想運行宏,請查看代碼,它創建了新的工作表等是它不精?

Range("A1").Select 
Sheets.Add 
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ 
"Sheet1!R1C1:R17445C24", Version:=xlPivotTableVersion12).CreatePivotTable _ 
TableDestination:="Sheet4!R3C1", TableName:="PivotTable1", DefaultVersion _ 
:=xlPivotTableVersion12 

Sheets("Sheet4").Select 
Cells(3, 1).Select 
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Bnlunit") 
    .Orientation = xlPageField 
    .Position = 1 
End With 
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Period") 
    .Orientation = xlColumnField 
    .Position = 1 
End With 
ActiveWindow.SmallScroll Down:=12 
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables(_ 
"PivotTable1").PivotFields("Amount"), "Sum of Amount", xlSum 
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Hdaccount_agr_3(T)") 
    .Orientation = xlRowField 
    .Position = 1 
End With 
ActiveWindow.SmallScroll Down:=-33 

End Sub 

回答

0

問題是因爲工作表名稱,而不是刪除這些表,我認爲下面的代碼可能是你的幫助

Sub Macro1() 
' 
Dim PSheet As Worksheet 
Dim DSheet As Worksheet 
Dim PCache As PivotCache 
Dim PTable As PivotTable 
Dim PRange As Range 
Dim LastRow As Long 
Dim LastCol As Long 

'Delete Preivous Pivot Table Worksheet & Insert a New Blank Worksheet With Same Name 
On Error Resume Next 
Application.DisplayAlerts = False 
Worksheets("PivotTable").Delete 
Sheets.Add Before:=ActiveSheet 
ActiveSheet.Name = "PivotTable" 
Application.DisplayAlerts = True 
Set PSheet = Worksheets("PivotTable") 
Set DSheet = Worksheets("Raw Data") 

'Define Data Range 
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row 
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column 
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol) 

'Define Pivot Cache 
Set PCache = ActiveWorkbook.PivotCaches.Create _ 
(SourceType:=xlDatabase, SourceData:=PRange). _ 
CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _ 
TableName:="PRIMEPivotTable") 

'Insert Blank Pivot Table 
Set PTable = PCache.CreatePivotTable_(TableDestination:=PSheet.Cells(1, 1), TableName:="PRIMEPivotTable") 

'Insert Column Fields 
'With ActiveSheet.PivotTables("PRIMEPivotTable").PivotFields("ColumnName") 
'.Orientation = xlColumnField 
'.Position = 1 
'End With 

'Insert Row Fields 
With ActiveSheet.PivotTables("PRIMEPivotTable").PivotFields("RowName") 
.Orientation = xlRowField 
.Position = 1 
End With 

'Insert Data Field 
With ActiveSheet.PivotTables("PRIMEPivotTable").PivotFields("Field 1") 
.Orientation = xlDataField 
.Position = 1 
.Function = xlCount 
.Name = "Name of your choice" 
End With 


End Sub 
+0

在設置'PTable'後,爲什麼不修改'PivotFields'時不要在下面使用它?爲什麼不使用'With PTable',並將其他對象嵌套在裏面? –

+0

同意,它不是一段精美的代碼(我們都喜歡看)。只是我有這段代碼可以與我們的團隊成員共享,所以我很容易進行進一步的修改。 –

0

嘗試下面的代碼,代碼的註釋中解釋:

Option Explicit 

Sub VBAPivot() 

Dim Sht1 As Worksheet 
Dim NewSht As Worksheet 
Dim PvtCache As PivotCache 
Dim PvtTbl As PivotTable 

Dim PvtRange As Range 
Dim LastRow As Long 

Set NewSht = ThisWorkbook.Sheets.Add ' add new sheet 

Set Sht1 = ThisWorkbook.Worksheets("Sheet1") 
With Sht1 
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 

    ' set the PivotCach DataSource Range 
    Set PvtRange = .Range("A1:X" & LastRow) 
End With 

' Set the Pivot Cache 
Set PvtCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, PvtRange.Address(False, False, xlA1, xlExternal)) 

' create a new Pivot Table in the new added sheet, in "A3" 
Set PvtTbl = NewSht.PivotTables.Add(PivotCache:=PvtCache, TableDestination:=NewSht.Range("A3"), TableName:="PivotTable1") 

With PvtTbl ' modify Pivot-Table properties 

    With .PivotFields("Bnlunit") 
     .Orientation = xlPageField 
     .Position = 1 
    End With 
    With .PivotFields("Period") 
     .Orientation = xlColumnField 
     .Position = 1 
    End With 

    ' add Field as Sum of 
    .AddDataField .PivotFields("Amount"), "Sum of Amount", xlSum 

    With .PivotFields("Hdaccount_agr_3(T)") 
     .Orientation = xlRowField 
     .Position = 1 
    End With 
End With 

End Sub