在vba中创建多透视表

在vba中创建多透视表,vba,excel,Vba,Excel,我已经使用VBA成功地创建了一个透视表,但现在我想在两个单独的工作表2和3中创建两个透视表。但我的脚本化VBA只创建一个透视表,尽管我已经设置了两个透视表变量:PT1和PT2,以及两个透视缓存变量:PTCache1和PTCache2。原始数据在工作表1中,我将其设置为RawData。 下面是我的脚本。请帮我找出我在这里遗漏了什么。谢谢 Private Sub CreatePivotTables() Dim ReportC2 As Workbook, RawData As Worksheet, S

我已经使用VBA成功地创建了一个透视表,但现在我想在两个单独的工作表2和3中创建两个透视表。但我的脚本化VBA只创建一个透视表,尽管我已经设置了两个透视表变量:PT1和PT2,以及两个透视缓存变量:PTCache1和PTCache2。原始数据在工作表1中,我将其设置为RawData。
下面是我的脚本。请帮我找出我在这里遗漏了什么。谢谢

Private Sub CreatePivotTables()
Dim ReportC2 As Workbook, RawData As Worksheet, SanityA As Worksheet
Dim LastRow As Long, LastCol As Long, i As Long
Dim PTCache1 As PivotCache, PTCache2 As PivotCache, PT1 As PivotTable, PT2 As PivotTable

Set ReportC2 = Workbooks.Open(ScorecardAddr & "\" & C2Name)
Set RawData = ReportC2.ActiveSheet

With RawData
    Columns("A:A").Insert
    .Range("A1").Value = "Deal & SKU"
    LastRow = .Range("B" & Rows.Count).End(xlUp).Row
    LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column


    For i = 2 To LastRow
        .Range("A" & i) = .Range("F" & i).Value & "|" & .Range("P" & i).Value
    Next

End With

With RawData

    Set PTCache1 = ActiveWorkbook.PivotCaches.Create(xlDatabase, Range(Cells(1, 1), Cells(LastRow, LastCol)))
    Set PTCache2 = ActiveWorkbook.PivotCaches.Create(xlDatabase, Range(Cells(1, 1), Cells(LastRow, LastCol)))
End With

On Error Resume Next
With ReportC2

.Sheets.Add After:=.Sheets(.Sheets.Count), Count:=2
.Sheets(2).Name = "C2Pivot-Transactional"

    Set PT1 = .Sheets(2).PivotTables.Add(PTCache1, Range("A7"), "C2PT1")

    'put the fields in the pivot table
    With PT1
        .PivotFields("Deal & SKU").Orientation = xlRowField
        .PivotFields("quantity").Orientation = xlDataField
        .PivotFields("GrossSellto").Orientation = xlDataField
        .PivotFields("Total BDD Rebate").Orientation = xlDataField
        .PivotFields("Total FLCP Rebate").Orientation = xlDataField
    End With
End With

With ReportC2

    .Sheets(3).Name = "C2Pivot-Reseller"

    Set PT2 = .Sheets(3).PivotTables.Add(PTCache2, Range("A7"), "C2PT2")

    'put the fields in the pivot table
    With PT2
        .PivotFields("Reseller ID").Orientation = xlRowField
        .PivotFields("GrossSellto").Orientation = xlDataField
        .CalculatedFields.Add "BDD + FLCP", "= 'Total BDD Rebate' + 'Total FLCP Rebate'"
        .PivotFields("BDD + FLCP").Orientation = xlDataField
    End With

End With

End Sub

首先,尽量不要使用
。选择
,而要明确参考您的工作簿和工作表。因此,您的数据透视表都是在
范围(“A7”)
中添加的,所以都在同一个位置。尝试从WB.WS.Range开始引用,其中WB是存储工作簿的变量,WS是存储WS的变量

编辑:作为参考,这里有一个关于如何避免使用
的问题和答案。选择

编辑2: 修复了你的代码(希望如此)

Edit3:用…结尾加回,并将两个数据透视缓存放在with循环中,同时删除PT2变量作为副本,最后在两个PT table加载项之间合并“with ReportC2”。这样就可以了

Private Sub CreatePivotTables()
Dim ReportC2 As Workbook, RawData As Worksheet, SanityA As Worksheet
Dim LastRow As Long, LastCol As Long, i As Long
Dim PTCache1 As PivotCache, PTCache2 As PivotCache, PT As PivotTable

Set ReportC2 = Workbooks.Open(ScorecardAddr & "\" & C2Name)
Set RawData = ReportC2.Worksheets(1)

With RawData
    Columns("A:A").Insert
    .Range("A1").Value = "Deal & SKU"
    LastRow = .Range("B" & Rows.Count).End(xlUp).Row
    LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column


    For i = 2 To LastRow
        .Range("A" & i) = .Range("F" & i).Value & "|" & .Range("P" & i).Value
Next

End With
with RawData
    Set PTCache1 = ActiveWorkbook.PivotCaches.Create(xlDatabase, Range(Cells(1, 1), Cells(LastRow, LastCol)))
    Set PTCache2 = ActiveWorkbook.PivotCaches.Create(xlDatabase, Range(Cells(1, 1), Cells(LastRow, LastCol)))
End With

On Error Resume Next
With ReportC2

.Sheets.Add After:=.Sheets(.Sheets.Count), Count:=2
.Sheets(2).Name = "C2Pivot-Transactional"

    Set PT = .Sheets(2).PivotTables.Add(PTCache1, Worksheets(2).Range("A7"), "C2PT1")

    'put the fields in the pivot table
    With PT
        .PivotFields("Deal & SKU").Orientation = xlRowField
        .PivotFields("quantity").Orientation = xlDataField
        .PivotFields("GrossSellto").Orientation = xlDataField
        .PivotFields("Total BDD Rebate").Orientation = xlDataField
        .PivotFields("Total FLCP Rebate").Orientation = xlDataField
    End With

    .Sheets(3).Name = "C2Pivot-Reseller"

    Set PT2 = .Sheets(3).PivotTables.Add(PTCache2, Worksheets(3).Range("A7"), "C2PT2")

    'put the fields in the pivot table
    With PT2
        .PivotFields("Reseller ID").Orientation = xlRowField
        .PivotFields("GrossSellto").Orientation = xlDataField
        .CalculatedFields.Add "BDD + FLCP", "= 'Total BDD Rebate' + 'Total FLCP Rebate'"
        .PivotFields("BDD + FLCP").Orientation = xlDataField
    End With

End With

End Sub

这对我来说适用于假设数据。我抛弃了PTCache2,一个就足够了

Private Sub CreatePivotTables()
Dim ReportC2 As Workbook, RawData As Worksheet, SanityA As Worksheet
Dim LastRow As Long, LastCol As Long, i As Long
Dim PTCache1 As PivotCache, PT As PivotTable, Pt2 As PivotTable

    Set ReportC2 = ActiveWorkbook 'Workbooks.Open(ScorecardAddr & "\" & C2Name)
    Set RawData = ReportC2.Worksheets(1)

    With RawData

        .Columns("A:A").Insert
        .Range("A1").Value = "Deal & SKU"
        LastRow = .Range("B" & .Rows.Count).End(xlUp).Row
        LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column


        For i = 2 To LastRow
            .Range("A" & i) = .Range("F" & i).Value & "|" & .Range("P" & i).Value
        Next

        Set PTCache1 = ActiveWorkbook.PivotCaches.Create(xlDatabase, .Range(.Cells(1, 1), .Cells(LastRow, LastCol)))
    End With

    With ReportC2

    .Sheets.Add After:=.Sheets(.Sheets.Count), Count:=2
    .Sheets(2).Name = "C2Pivot-Transactional"

        Set PT = .Sheets(2).PivotTables.Add(PTCache1, Worksheets(2).Range("A7"), "C2PT1")

        'put the fields in the pivot table
        With PT
            .PivotFields("Deal & SKU").Orientation = xlRowField
            .PivotFields("quantity").Orientation = xlDataField
            .PivotFields("GrossSellto").Orientation = xlDataField
            .PivotFields("Total BDD Rebate").Orientation = xlDataField
            .PivotFields("Total FLCP Rebate").Orientation = xlDataField
        End With

        .Sheets(3).Name = "C2Pivot-Reseller"

        Set Pt2 = .Sheets(3).PivotTables.Add(PTCache1, Worksheets(3).Range("A7"), "C2PT2")

        'put the fields in the pivot table
        With Pt2
            .PivotFields("Reseller ID").Orientation = xlRowField
            .PivotFields("GrossSellto").Orientation = xlDataField
            .CalculatedFields.Add "BDD + FLCP", "= 'Total BDD Rebate' + 'Total FLCP Rebate'"
            .PivotFields("BDD + FLCP").Orientation = xlDataField
        End With
    End With
End Sub

您好,我试过了。我用表示“ReportC2.ActiveSheet”的RawData替换它;并用…结尾,但仍然失败。我的代码有什么问题吗?谢谢。@lukayl我为您做了更改,让我知道它是否有效。您好@Luuklag,第一次尝试给我错误,似乎是RawData。PivotCaches.Create(xlDatabase,Range(Cells(1,1),Cells(LastRow,LastCol)))不起作用。所以我仍然添加了With RawData,…在两个PivotCaches部分中以循环结束,然后在With循环中使用ActiveWorkbook而不是RawData。删除额外的PT2变量,因为我发现一个PT也很好。然后将Pivot表部分中的两个With ReportC2循环合并为一个,以避免出现叮当声。这一切都有效。比ks.我打赌你也可以只使用一个PTCache,因为它们都是相同的范围。你应该删除On Error Goto Next,看看会出现什么错误。你是否尝试过注释
设置PTCache2…
并且只对两个透视表使用PTCache1?@BobPhillips不幸的是,我是Excel VBA的Noob,错误消息对我来说意义不大e、 这些代码实际上是我在YouTube上找到的,只是做了一些小的调整以适应我的数据集。谢谢。告诉我们错误是什么,在哪一行,也许我们可以帮上忙。