在VBA

2015-12-21 64 views
1

创建多个数据透视表我已经成功地通过使用VBA创建的数据透视表,但现在我想在两个单独的工作表,2和3。但是,创建两个数据透视表我的脚本VBA只创建一个数据透视表,甚至尽管我已经设置了2个透视表变量:PT1和PT2以及2个透视缓存变量:PTCache1和PTCache2。原始数据在工作表1中,我将其设置为RawData。
下面是我的脚本。请帮我解决我在这里失踪的事情。谢谢。在VBA

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 
+0

你应该删除On Error Goto Next,看看你得到了什么错误。 –

+0

您是否尝试过注释'Set PTCache2 ...',并且仅对两个数据透视表使用PTCache1? –

+0

@BobPhillips不幸的是,我为Excel VBA一个小白,错误消息意味着没有多少给我。这些代码实际上就是我在YouTube中发现的一些小调整,以适合我的数据集。谢谢。 – lukayl

回答

1

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

编辑:作为参考,在这里一个问题,答案,就如何避免使用.SelectHow to avoid using Select in Excel VBA macros

EDIT2: 固定你的代码(希望)

EDIT3:添加回用.. .END用,并把两者的PivotCaches在带环,还删除PT2变量重复,最后合并“随着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 
+0

嗨,我试过了。我替换RawData表示“ReportC2.ActiveSheet”;并使用With ... End With,但仍然失败。我的代码有什么问题?谢谢。 – lukayl

+0

@lukayl我为你做了修改,让我知道它是否有效。 – Luuklag

+0

嗨@Luuklag,第一次尝试给了我的错误,似乎RawData.PivotCaches.Create(xlDatabase,范围(单元格(1,1),电池(LASTROW,LASTCOL)))不起作用。所以我仍然在两个PivotCaches部分添加With RawData,... End With循环,然后在With循环中使用ActiveWorkbook代替RawData。删除额外的PT2变量,因为我发现一个PT也很好。然后将两个与Pivot Table部分中的ReportC2循环合并为一个以避免出现严重的问题。它一切正常。谢谢。 – lukayl

0

这对我的作品假设d数据。我放弃了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 
+0

是的,它的工作。谢谢。 – lukayl

+0

你可以将它标记为解决方案吗? –

+0

我只是勾选了这个标记。谢谢。 – lukayl