2017-10-17 44 views
0

我創建了一個宏,通過索引匹配和一般計算創建一個包含一些值的表。此表格位於範圍內(「AA1:BA」& LastRow) - >我正在寫LastRow,因爲拉斯特羅每天都在變化。我想創建一個數據透視表,它位於單元格(9,1)中,並且將創建一個我創建的初始表作爲源代碼。我構建了您在下面看到的代碼,但它給了我一個錯誤,數據透視表字段名稱在行上無效:自動創建數據透視表時出錯

Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange).CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), TableName:="PivotTable") 

與數據透視表相關的整個代碼如下。

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 

Application.DisplayAlerts = False 
Application.DisplayAlerts = True 
Set PSheet = Worksheets("Budget_Report") 
Set DSheet = Worksheets("Budget_Report") 

LastRow = DSheet.Cells(Rows.Count, 27).End(xlUp).Row 
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column 

Set PRange = DSheet.Cells(1, 27).Resize(LastRow, LastCol) 

Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange) 
Set PTable = PCache.CreatePivotTable(TableDestination:=PSheet.Cells(9, 1), TableName:="PivotTable") 

With ActiveSheet.PivotTables("PivotTable").PivotFields("T-Lane") 
.Orientation = xlRowField 
.Position = 1 
.Subtotals(1) = True 
.Subtotals(1) = False 
End With 

With ActiveSheet.PivotTables("PivotTable").PivotFields("Monthly Cost FCST") 
.Orientation = xlDataField 
.Position = 1 
.Function = xlSum 
.Name = "Monthly Cost Forecast" 
End With 
With ActiveSheet.PivotTables("PivotTable").PivotFields("Monthly Vol FCST") 
.Orientation = xlDataField 
.Position = 2 
.Function = xlSum 
.Name = "Monthly Vol Forecast" 
End With 
With ActiveSheet.PivotTables("PivotTable").PivotFields("Monthly $/SU FCST") 
.Orientation = xlDataField 
.Position = 3 
.Function = xlSum 
.Name = "Monthly $/SU Forecast" 
End With 
With ActiveSheet.PivotTables("PivotTable").PivotFields("Monthly Cost Actuals") 
.Orientation = xlDataField 
.Position = 4 
.Function = xlSum 
.Name = "Monthly Cost Actual" 
End With 
With ActiveSheet.PivotTables("PivotTable").PivotFields("Monthly Vol Actuals") 
.Orientation = xlDataField 
.Position = 5 
.Function = xlSum 
.Name = "Monthly Vol Actual" 
End With 
With ActiveSheet.PivotTables("PivotTable").PivotFields("Monthly $/SU Actuals") 
.Orientation = xlDataField 
.Position = 6 
.Function = xlSum 
.Name = "Monthly $/SU Actual" 
End With 

錄製宏,建立數據透視表的下方設置(它有特定的範圍不LASTROW當然lastcolumn)

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="Budget_Report!R1C27:R279C53", Version:=6).CreatePivotTable TableDestination:="Budget_Report!R9C1", TableName:="PivotTable1", DefaultVersion:=6 
Sheets("Budget_Report").Cells(9, 1).Select 
With ActiveSheet.PivotTables("PivotTable1").PivotFields("T-Lane") 
    .Orientation = xlRowField 
    .Position = 1 
End With 
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables("PivotTable1").PivotFields("Monthly Cost FCST"), "Monthly Cost Forecast", xlSum 
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables("PivotTable1").PivotFields("Monthly Vol FCST"), "Monthly Vol Forecast", xlSum 
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables("PivotTable1").PivotFields("Monthly $/SU FCST"), "Monthly $/SU Forecast", xlSum 
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables("PivotTable1").PivotFields("Monthly Cost Actuals"), "Monthly Cost Actual", xlSum 
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables("PivotTable1").PivotFields("Monthly Vol Actuals"), "Monthly Vol Actual", xlSum 
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables("PivotTable1").PivotFields("Monthly $/SU Actuals"), "Monthly $/SU Actual", xlSum 

ActiveWorkbook.ShowPivotTableFieldList = False 
+0

如果沒有錯誤,那麼將是數據透視表的字段名? – jsotola

+0

嗯我很抱歉jsotola,但我真的不明白這個問題..我不是很有經驗的數據透視表,所以我不能真正回答這個問題:(我剛剛看到代碼在線,並試圖實現它爲我的宏。 –

+0

你實際上有數據可以被饋送到數據透視表中...如果你這樣做,然後手動創建數據透視表,excel應該抱怨字段名稱,它可能會指出你的問題 – jsotola

回答

1

設置當PRange可以使用Cells.SpecialCells方法來設置你的範圍內,即使你並不總是知道最終地址會是什麼,只要你確定在目標範圍以下/以外的地方不會有額外的數據。

這將是這個樣子:

Set PRange = DSheet.Range(DSheet.Cells(1, 27), DSheet.Cells.SpecialCells(xlCellTypeLastCell)) 

而且,它是個人風格問題,但我發現嵌套With塊有時會導致更可讀的代碼。例如:

With ActiveSheet.PivotTables("PivotTable1") 
    With .PivotFields("Field 1") 
     .Caption = "My first field" 
     .Function = xlSum 
    End With 
    With .PivotFields("Field 2") 
     .Caption = "My second field" 
     .Function = xlMax 
    End With 
End With 

最終產品看起來是這樣的:

Dim DSheet As Worksheet ', PSheet As Worksheet 
Dim PCache As PivotCache, PTable As PivotTable, PRange As Range 
'Dim LastRow As Long, LastCol As Long 

'Application.DisplayAlerts = False ' This is redundant since you're setting it to true below. 
Application.DisplayAlerts = True 
'Set PSheet = Worksheets("Budget_Report") ' 
Set DSheet = Worksheets("Budget_Report") ' These two are Redundant - I am using only DSheet. 

'LastRow = DSheet.Cells(Rows.Count, 27).End(xlUp).Row 
'LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column 

Set PRange = DSheet.Range(Cells(1, 27),Cells.SpecialCells(xlCellTypeLastCell)) 

Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange) 
Set PTable = PCache.CreatePivotTable(TableDestination:=DSheet.Cells(9, 1), TableName:="PivotTable") 

With ActiveSheet.PivotTables("PivotTable") 
With .PivotFields("T-Lane") 
    .Orientation = xlRowField 
    .Position = 1 
    .Subtotals(1) = True ' You're resetting this value in the next  line, you can probably remove it. 
    .Subtotals(1) = False 
End With 

With .PivotFields("Monthly Cost FCST") 
.Orientation = xlDataField 
.Position = 1 
.Function = xlSum 
.Caption = "Monthly Cost Forecast" 'You want .Caption here, not .Name 
End With 

With .PivotFields("Monthly Vol FCST") 
.Orientation = xlDataField 
.Position = 2 
.Function = xlSum 
.Caption = "Monthly Vol Forecast" 
End With 

With .PivotFields("Monthly $/SU FCST") 
.Orientation = xlDataField 
.Position = 3 
.Function = xlSum 
.Caption = "Monthly $/SU Forecast" 
End With 

With .PivotFields("Monthly Cost Actuals") 
.Orientation = xlDataField 
.Position = 4 
.Function = xlSum 
.Caption = "Monthly Cost Actual" 
End With 

With .PivotFields("Monthly Vol Actuals") 
.Orientation = xlDataField 
.Position = 5 
.Function = xlSum 
.Caption = "Monthly Vol Actual" 
End With 

With .PivotFields("Monthly $/SU Actuals") 
.Orientation = xlDataField 
.Position = 6 
.Function = xlSum 
.Caption = "Monthly $/SU Actual" 
End With 

End With 
+1

很高興聽到它爲你工作。當心,在MSDN上的PivotCaches.Create文章指出_When傳遞範圍,建議要麼使用字符串指定工作簿,工作表和單元格範圍,或建立一個命名範圍,並通過名稱爲字符串。傳遞一個Range對象可能會導致「類型不匹配」錯誤unexpectedly._ [鏈接](https://msdn.microsoft.com/en-us/vba/excel-vba/articles/pivotcaches-create-method-excel) –

+0

抱歉再次打擾你的朋友..如果我想在同一張表中添加同一個sourcedata的第二個表(但是當然有不同的顯示數據),我應該在代碼中更改什麼?我設置一個新的PTable2,我只是改變了數據透視表作爲PivotTable2的名稱和它說,它不能覆蓋透視表 –

+0

您還需要指定一個新的地方把它 - 你不能有一個數據透視表上的頂部另一個。 'TableDestination:= ...' –

0

看來,您嘗試創建同一個表兩次:

Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange).CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), TableName:="PivotTable") 
Set PTable = PCache.CreatePivotTable(TableDestination:=PSheet.Cells(9, 1), TableName:="PivotTable") 

在兩人只是把它分解:

Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange) 
Set PTable = PCache.CreatePivotTable(TableDestination:=PSheet.Cells(9, 1), TableName:="PivotTable") 
+0

它給我上設置PTABLE線自動化錯誤.. –

+0

@PericlesFaliagas用'設置PTABLE線...'肯定是正確的。因此,無論您是否已經擁有一個具有該名稱的數據透視表,目標有問題或「PivotCache」未正確創建。 –

+0

它實際上是我在這本練習冊上唯一的數據透視表!是否可以使用作爲數據源的數據在同一工作表中創建數據透視表? –

0

分配到PCACHE只緩存:

Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange) 
+0

它給我設置PTable的線路上的自動化錯誤 –