VBA数据透视表彼此低于

时间:2015-08-21 20:19:11

标签: excel vba excel-vba

我在电路板上寻找解决方案,但我找不到类似的东西。

我正在尝试放置大约5个数据透视表,其中不同的数据行彼此相同。我能够用前两个完成它,但代码不能从第三个开始。

可悲的是,我被要求将它们垂直放在纸张上。下面是我的一段代码。抱歉这个烂摊子,但我对VBA有点新意。我正在使用Excel 2007。

    Sheets.ADD.Name = "Pivot"

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "CTB!" & Sheets("CTB").Range("A1").CurrentRegion.Address(ReferenceStyle:=xlR1C1), Version:=xlPivotTableVersion14).CreatePivotTable _
    TableDestination:="Pivot!R1C1", TableName:="PivotTable1", DefaultVersion _
    :=xlPivotTableVersion14

Cells(1, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Collector")
    .Orientation = xlRowField
    .Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("Transaction #"), "Count of Transaction #", xlCount
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("Invoice Amount"), "Sum of Invoice Amount", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("Total Balance"), "Sum of Total Balance", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("Days Late"), "Sum of Days Late", xlSum
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of Days Late")
    .Caption = "Average of Days Late"
    .Function = xlAverage
End With

Dim pt As PivotTable
Set pt = Sheets("Pivot").PivotTables("PivotTable1")

Application.PivotTableSelection = True
pt.PivotSelect ("Sum of Total Balance"), xlDataOnly
Selection.Style = "Currency"
pt.PivotSelect ("Sum of Invoice Amount"), xlDataOnly
Selection.Style = "Currency"
pt.PivotSelect ("Average of Days Late"), xlDataOnly
Selection.NumberFormat = "0"

Dim LastRow As Long
LastRow = Sheets("Pivot").Cells(Sheets("Pivot").Rows.Count, "A").End(xlUp).Row

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "C121+!" & Sheets("C121+").Range("A1").CurrentRegion.Address(ReferenceStyle:=xlR1C1), Version:=xlPivotTableVersion14).CreatePivotTable _
    TableDestination:=Range("A" & LastRow + 2), TableName:="PivotTable2", DefaultVersion _
    :=xlPivotTableVersion14

Dim pt2 As PivotTable
Set pt2 = Sheets("Pivot").PivotTables("PivotTable2")

pt2.TableRange1.Select
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Collector")
    .Orientation = xlRowField
    .Position = 1
End With
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
    "PivotTable2").PivotFields("Transaction #"), "Count of Transaction #", xlCount
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
    "PivotTable2").PivotFields("Invoice Amount"), "Sum of Invoice Amount", xlSum
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
    "PivotTable2").PivotFields("Total Balance"), "Sum of Total Balance", xlSum
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
    "PivotTable2").PivotFields("Days Late"), "Sum of Days Late", xlSum
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Sum of Days Late")
    .Caption = "Average of Days Late"
    .Function = xlAverage
End With

Application.PivotTableSelection = True
pt2.PivotSelect ("Sum of Total Balance"), xlDataOnly
Selection.Style = "Currency"
pt2.PivotSelect ("Sum of Invoice Amount"), xlDataOnly
Selection.Style = "Currency"
pt2.PivotSelect ("Average of Days Late"), xlDataOnly
Selection.NumberFormat = "0"

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "11+!" & Sheets("11+").Range("A1").CurrentRegion.Address(ReferenceStyle:=xlR1C1), Version:=xlPivotTableVersion14).CreatePivotTable _
    TableDestination:=Range("A" & LastRow + 2), TableName:="PivotTable3", DefaultVersion _
    :=xlPivotTableVersion14

Dim pt3 As PivotTable
Set pt3 = Sheets("Pivot").PivotTables("PivotTable3")

pt3.TableRange1.Select
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Collector")
    .Orientation = xlRowField
    .Position = 1
End With
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
    "PivotTable3").PivotFields("Transaction #"), "Count of Transaction #", xlCount
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
    "PivotTable3").PivotFields("Invoice Amount"), "Sum of Invoice Amount", xlSum
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
    "PivotTable3").PivotFields("Total Balance"), "Sum of Total Balance", xlSum
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
    "PivotTable3").PivotFields("Days Late"), "Sum of Days Late", xlSum
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Sum of Days Late")
    .Caption = "Average of Days Late"
    .Function = xlAverage
End With

Application.PivotTableSelection = True
pt3.PivotSelect ("Sum of Total Balance"), xlDataOnly
Selection.Style = "Currency"
pt3.PivotSelect ("Sum of Invoice Amount"), xlDataOnly
Selection.Style = "Currency"
pt3.PivotSelect ("Average of Days Late"), xlDataOnly
Selection.NumberFormat = "0"

任何建议都将不胜感激!

1 个答案:

答案 0 :(得分:1)

看起来每次要插入新的pivotTable时都必须更新LastRow变量。然后下一个pivotTable将插入到最后一行下面两行开始的范围内......

在插入第二个pivotTable之前设置LastRow变量一次,但是在插入pivotTable3等之前必须重置它以读取当前的最后一行