2016-05-09 2 views
1

データをプルし、データからピボットテーブルを作成するvbaコードがあります。今起こっていることは、ピボットテーブルを作成するためにボタンを押すたびに、別の番号の新しいシートに入れられることです。私がしたいのは、私が作成した新しいシートに入れられたピボットテーブルです。私は以下に書いたコードを加えました。前もって感謝します。追加されたシートにピボットテーブルを配置する

On Error Resume Next 
Set pc = src.PivotCaches.Create(xlDatabase, Range("A1").CurrentRegion) 

Set wsNew = Sheets.Add 
wsNew.Name = "DAM-AWW Pivot" 

Set objtable = ws.PivotTableWizard(, , , , False) 
objtable.Name = "DAM-AWW Pivot" 

答えて

0

VBAを使用してピボットテーブルを作成すると、ウィザードは使用されません。私は、PivotCacheを作成してからPivotTableを作成するという2つのステップで、PTを正確に配置するのに柔軟性があることを発見しました。

以下

は、私はどちらかの更新にすべての時間を使用するか、新しいPivotTableを作成する機能のカット/ペーストである:

Private Sub UpdatePivotTable(ptwb As Workbook, ptName As String, ptRange As Range) 
    On Error GoTo Err_UpdatePivotTable 

    '--- refreshes the data in the given pivot table for the (potentially) 
    ' expanded data range; creates the pivot table if it doesn't exist 
    Dim thisPivot As PivotTable 
    Dim ptCache As PivotCache 
    Dim ptSheet As Worksheet 
    Dim ptField As PivotField 
    Dim ptshname As String 

    '--- make sure the destination workbook and sheet are correctly 
    ' set up and ready 
    ptshname = ptName & " Pivot" 
    Set ptSheet = ptwb.Sheets(ptshname) 

    '--- does the pivot table even exist? 
    On Error Resume Next 
    Set thisPivot = ptSheet.PivotTables(ptName) 
    On Error GoTo Err_UpdatePivotTable 

    If thisPivot Is Nothing Then 
     Set ptCache = ptwb.PivotCaches.Create(SourceType:=xlDatabase, _ 
               SourceData:=ptRange, _ 
               version:=xlPivotTableVersion12) 
     Set thisPivot = ptCache.CreatePivotTable(TableDestination:=ptSheet.Range("B9"), _ 
               TableName:=ptName, _ 
               DefaultVersion:=xlPivotTableVersion12) 
     With thisPivot 
      Set ptField = .PivotFields("ProjectID") 
      ptField.Orientation = xlRowField 
      ptField.Position = 1 
      Set ptField = .PivotFields("ProjectName") 
      ptField.Orientation = xlRowField 
      ptField.Position = 2 
      Set ptField = .PivotFields("LaborMonth") 
      ptField.Orientation = xlColumnField 
      ptField.Position = 1 
      Set ptField = .AddDataField(.PivotFields("Hours"), "Sum of Hours", xlSum) 
      .InGridDropZones = True 
      .RowAxisLayout xlTabularRow 
      For Each ptField In .PivotFields 
       ptField.Subtotals(1) = True 
       ptField.Subtotals(1) = False 
      Next ptField 
      .DataBodyRange.NumberFormat = "0.00;;-" 
      .DataBodyRange.HorizontalAlignment = xlCenter 
      .ColumnRange.HorizontalAlignment = xlCenter 
      .TableStyle2 = "PivotStyleMedium9" 
     End With 
    Else 
     thisPivot.ChangePivotCache ptwb.PivotCaches.Create(SourceType:=xlDatabase, _ 
                  SourceData:=ptRange, _ 
                  version:=xlPivotTableVersion12) 
     thisPivot.PivotCache.Refresh 
    End If 
    thisPivot.PivotFields("LaborMonth").AutoSort xlAscending, "LaborMonth" 

Exit_UpdatePivotTable: 
    On Error GoTo 0 
    Exit Sub 

Err_UpdatePivotTable: 
    MsgBox "Error " & Err.Number & " (" & Err.Description & _ 
      ") in procedure UpdatePivotTable of " & _ 
      "Module CostpointLabor", vbOKOnly 
    Resume Exit_UpdatePivotTable 
End Sub 

明らかにこれのほとんどは、特定のニーズに合わせるために使用する例です。

+0

私は、コーディングに関しては、ブロックの上に新しい子供がいます。あなたのコードに含まれていることをすべて理解していません。現在、コードを公開しています。ピボットを作る...私はちょうどプライベートサブを作って、データが変更された後パブリックサブでそれを呼び出す必要がありますか? –

+0

申し訳ありませんが、混乱する点でした。私が利用可能な数分で答えを出すために、私はプライベートである既存の関数をカット/ペーストしました。あなたのために、 'Set objtable = ws.PivotTableWizard ...'で始まる2行を置き換え、 'Set objtable = pc.CreatePivotTable(wsNew.Range(" A10 ")、" DAM-AWW Pivot ")という行で置き換えます。 '。それがどこから去るかを見てください。 – PeterT

+0

私はそれを試してピボットテーブルが作られていません...新しいシートが現れても何も起こらない –

関連する問題