在vba中创build多个数据透视表

我已经使用VBA成功创build了一个数据透视表,但是现在我想在两个单独的工作表中创build两个数据透视表,2和3.但是我的脚本VBA只创build了一个数据透视表,即使我已经设置了2个数据透视表variables: PT1和PT2以及2个数据透视cachingvariables: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 

首先尝试不使用.Select而是尝试明确引用您的工作簿和工作表。 正因为如此,你的数据透视表都被添加到Range("A7") ,所以在同一个地方。 尝试引用以WB.WS.Range开头,其中WB是存储工作簿的variables,WS是存储WS的variables。

编辑:作为参考,这里有一个问题和答案,关于如何避免使用。select : 如何避免使用在Excel中selectVBAmacros

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

Edit3:用…结尾回来,并将两个pivotcaches放在With循环中,同时删除PT2variables作为重复项,最后合并“With ReportC2在两个PT表插件之间。

 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