2017-10-17 46 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的线路上的自动化错误 –