0

コードは機能しています! 助けてくれてありがとう!

行数が異なるデータで機能する動的ピボット テーブルを作成しようとしています。現在、28,300 行ありますが、これは毎日変わる可能性があります。

データ形式の例は次のとおりです。

Case Number    Branch      Driver
1342           NYC         Bob
4532           PHL         Jim
7391           CIN         John
8251           SAN         John
7211           SAN         Mary
9121           CLE         John
7424           CIN         John

完成したテーブルの例:

Driver    NYC    PHL   CIN   SAN   CLE
Bob       1      0     0     0     0
Jim       0      1     0     0     0    
John      0      0     2     1     1     
Mary      0      0     0     1     0     

次のようにコードします。

Sub CreateSummaryReportUsingPivot()
' Use a Pivot Table to create a static summary report
' with model going down the rows and regions across
Dim WSD As Worksheet
Dim PTCache As PivotCache
Dim PT As PivotTable
Dim PRange As Range
Dim FinalRow As Long
Dim FinalCol As Long
Set WSD = Worksheets("PivotTable")

'Name active worksheet as "PivotTable"
 ActiveSheet.Name = "PivotTable"

' Delete any prior pivot tables
For Each PT In WSD.PivotTables
    PT.TableRange2.Clear
Next PT

' Define input area and set up a Pivot Cache
FinalRow = WSD.Cells(Application.Rows.Count, 1).End(xlUp).Row
FinalCol = WSD.Cells(1, Application.Columns.Count). _
    End(xlToLeft).Column
Set PRange = WSD.Cells(1, 1).Resize(FinalRow, FinalCol)
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:= _
    xlDatabase, SourceData:=PRange)

' Create the Pivot Table from the Pivot Cache
Set PT = PTCache.CreatePivotTable(TableDestination:=WSD. _
    Cells(2, FinalCol + 2), TableName:="PivotTable1")

' Turn off updating while building the table
PT.ManualUpdate = True

' Set up the row fields
PT.AddFields RowFields:="Driver", ColumnFields:="Branch"

' Set up the data fields
With PT.PivotFields("Case Number")
    .Orientation = xlDataField
    .Function = xlCount
    .Position = 1
End With

With PT
    .ColumnGrand = False
    .RowGrand = False
    .NullString = "0"
End With

' Calc the pivot table
PT.ManualUpdate = False
PT.ManualUpdate = True

End Sub
4

2 に答える 2

2

彼らはPivotCachesのオブジェクトモデルを変更しました。2007〜2010年に必要な方法(バージョン6ではなくVBAバージョン7を使用)は次のとおりです。

 PivotCaches.Create
于 2012-09-28T13:20:48.987 に答える
1

行数の変更以外に VBA を使用している理由はありますか?

Excel 2007 / 2010 を使用している場合は、元のデータから通常のテーブル/リスト (Ctrl-L) を作成します。名前を付けることもできます。次に、ピボット テーブルを作成し、テーブル名をデータ ソースとして使用します。行を追加すると、テーブルが拡張され、ピボット テーブルを更新できます (F5 または VBA を使用)。

Excel 2003 を使用している場合は、動的な名前付き範囲も作成できます。少し複雑です (そしてかなり醜いです) が、古いバージョンで立ち往生している場合は、順を追って説明できます。

于 2012-09-25T15:55:38.757 に答える