2012-10-12 12 views
5

Tôi đang cố gắng tạo bảng Pivot nhưng nhận được Invalid Procedure Call or Argument.Cách tạo Bảng tổng hợp trong VBA

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="rng", Version:=xlPivotTableVersion14).CreatePivotTable TableDestination:="rngB", TableName:="pvtReportA_B", DefaultVersion:=xlPivotTableVersion14 
  • rng (Nguồn) là một phạm vi bao gồm khoảng 20 cột và vài ngàn dòng.
  • rngB (Điểm đến) là một tế bào duy nhất trong một bảng tính khác nhau

bất cứ ai có thể tư vấn cho nơi tôi đang đi sai?

EDIT:

lỗi của tôi, tôi cần phải có được sử dụng rngData và không rng như Source.

Set rng = wsA.Range("C14") 
    Set rngData = Range(rng, rng.End(xlToRight)) 
    Set rngData = Range(rng, rng.End(xlDown)) 
    Set rngB = wsB.Range("C8") 

    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngData, Version:=xlPivotTableVersion14).CreatePivotTable TableDestination:=rngB, TableName:="pvtReportA_B", DefaultVersion:=xlPivotTableVersion14 

Điều này sẽ trả về khung PivotTable tốt.

+0

vì vậy, tất cả đều tốt? Đã đóng câu hỏi? – nutsch

+0

Bạn có thể đặt phạm vi dữ liệu bằng một dòng mã: 'Đặt rngData = range (wsA.Range (" C14 "), wsA.Range (" C14 "). End (xlToRight) .end (xlDown))' –

Trả lời

4

Trong trường hợp này, tôi đã sử dụng đối tượng phạm vi sai khiến Excel phù hợp.

Set rng = wsA.Range("C14") 
Set rngData = Range(rng, rng.End(xlToRight)) 
Set rngData = Range(rng, rng.End(xlDown)) 
Set rngB = wsB.Range("C8") 

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngData, Version:=xlPivotTableVersion14).CreatePivotTable TableDestination:=rngB, TableName:="pvtReportA_B", DefaultVersion:=xlPivotTableVersion14 
+1

Trong dòng thứ ba của mã bạn nên đặt 'rngData' thay vì 'rng' – amg

3

Để tạo một trục trong Excel 2010, sử dụng mã VBA, bạn có thể sử dụng và thích ứng với mẫu này:

Sub newPVT() 
    Dim PTCache As PivotCache 
    Dim PT As PivotTable 

    'Create the Cache 
    Set PTCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _ 
     SourceData:=Range("Dynamic_Field_Summary")) 

    'Select the destination sheet 
    Sheets("Field Summary").Select 

    'Create the Pivot table 
    Set PT = ActiveSheet.PivotTables.Add(PivotCache:=PTCache, _ 
     TableDestination:=Range("P1"), TableName:="Pivot1") 

    ActiveWorkbook.ShowPivotTableFieldList = True 

    'Adding fields 
    With PT 
     With .PivotFields("Enterprise") 
      .Orientation = xlColumnField 
      .Position = 1 
     End With 

     With .PivotFields("Field") 
      .Orientation = xlRowField 
      .Position = 1 
     End With 

     With .PivotFields("Planted Acres") 
      .Orientation = xlDataField 
      .Position = 1 
      .Caption = " Planted Acres" 
      .Function = xlSum 
     End With 

     With .PivotFields("Harvested Acres") 
      .Orientation = xlDataField 
      .Position = 2 
      .Caption = " Harvested Acres" 
      .Function = xlSum 
     End With 

     With .PivotFields("lbs") 
      .Orientation = xlDataField 
      .Position = 3 
      .Caption = " lbs" 
      .Function = xlSum 
     End With 

     'Adjusting some settings 
     .RowGrand = False 
     .DisplayFieldCaptions = False 
     .HasAutoFormat = False 

     'Improving the layout 
     .TableStyle2 = "PivotStyleMedium9" 
     .ShowTableStyleRowStripes = True 
     .ShowTableStyleColumnStripes = True 

    End With 

    With ActiveSheet 
     'Adjusting columns width 
     .Columns("P:V").ColumnWidth = 16 
     .Range("Q2:V2").HorizontalAlignment = xlCenter 
    End With 

    ActiveWorkbook.ShowPivotTableFieldList = False 
End Sub 

tôi thấy nó here.

Trong this page bạn cũng có thể làm tốt ý nghĩa của mọi phần của mã, ví dụ: được giải thích here. Tôi nghĩ rằng đây cũng là một mã tốt để bắt đầu tạo macro vba cho Excel 2007 hoặc các phiên bản khác.