2016-09-14 4 views
0

と、自分の新しいワークシート上の各私はコードUが新しいピボットで新しいタブを作成し、列の各行にダイナミック名ここではVBA

をやりたいです、ループ内でピボットテーブルを作成しますテーブルは、マスターワークシートのデータに基づいて

私は信じているダイナミックテーブルの宛先です。私は "Master"ワークシートのリストに基づいた変数PayorNameを作成しています。ピボットテーブル上に "[会社]ピボット"という名前のシートが必要です

私は一度にあまりにも多くをやろうとしていますか?

部分コード:

Dim LastRowA As Long 
With ActiveSheet 
    LastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row 
End With 
Dim LastRowU As Long 
With ActiveSheet 
    LastRowU = .Cells(.Rows.Count, "U").End(xlUp).Row 
End With 


ActiveSheet.Name = "Master" 

'this loop is set to run the first 5 items temporarily 
'later it will go to lastrowU 
For i = 2 To 5 

PayorName = Sheets("Master").Cells(i, "U").Value 

Sheets.Add 
ActiveSheet.Name = PayorName & " Pivot" 
With ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ 
     "Master1!R1C1:R" & LastRowA) 
     .CreatePivotTable TableDestination:=Sheets(PayorName & " Pivot").Range("A3"), TableName:="PivotTable" 
End With 
    Sheets(PayorName & " Pivot").Select 
    Cells(3, 1).Select 
    ' from here I set the pivot's fields 
next i 

答えて

0

それはピボットキャッシュを動的に行うことには大きすぎるということだろうか?私はこのコードを試してみましたが、私は(... pvtcache設定)

Dim FinalRow   As Long 
Dim DataSheet   As String 
Dim PvtCache   As PivotCache 
Dim PvtTbl    As PivotTable 
Dim DataRng    As Range 
Dim TableDest   As Range 

Dim LastCol As Integer 
    With ActiveSheet 
     LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column 
    End With 
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row 
DataSheet = ActiveSheet.Name 

' set data range for Pivot Table 
Set DataRng = Sheets(DataSheet).Range(Cells(1, 1), Cells(FinalRow, LastCol)) 
' set pivot cache 
Set PvtCache = ActiveWorkbook.PivotCaches.Add(xlDatabase, DataRng) 

が続いて、最後の行にエラー(型の不一致)があるピボットキャッシュを設定しようとするとエラーが出るが、コードはように継続していますsuch

PayorName = Sheets("Master1").Cells(i, "U").Value 

Sheets.Add 
ActiveSheet.Name = PayorName & " Pivot" 

' set range for Pivot table placement 
Set TableDest = Sheets(PayorName & " Pivot").Cells(3, 1) 



' this line in case the Pivot table doesn't exit >> first time running this Macro 
On Error Resume Next 
Set PvtTbl = ActiveWorkbook.Sheets(DataSheet).PivotTables("PivotTable") ' check if "PivotTable4" Pivot Table already created (in past runs of this Macro) 

On Error GoTo 0 
If PvtTbl Is Nothing Then ' "PivotTable" doesn't exist >> create it 

    ' create a new Pivot Table in "PivotTable" sheet 
    Set PvtTbl = ActiveWorkbook.Sheets(DataSheet).PivotTables.Add(PivotCache:=PvtCache, TableDestination:=TableDest, TableName:="PivotTable") 

    ' setting fields 
    ActiveSheet.PivotTables("PivotTable").AddDataField ActiveSheet.PivotTables(_ 
      "PivotTable1").PivotFields("Amount"), "Sum of Amount", xlSum 
     With ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of Amount") 
      .NumberFormat = "$#,##0.00_);($#,##0.00)" 
     End With 
     With ActiveSheet.PivotTables("PivotTable").PivotFields("GL Debit Account") 
      .Orientation = xlColumnField 
      .Position = 1 
     End With 
     With ActiveSheet.PivotTables("PivotTable").PivotFields("Payment Payor") 
      .Orientation = xlRowField 
      .Position = 1 
     For x = 1 To .PivotItems.Count - 1 
      .PivotItems(.PivotItems(x).Name).Visible = False 
     Next x 
     .PivotItems(PayorName).Visible = True 
     End With 
     With ActiveSheet.PivotTables("PivotTable").PivotFields("Post Date") 
      .Orientation = xlRowField 
      .Position = 2 
     End With 
     With ActiveSheet.PivotTables("PivotTable").PivotFields("Batch #") 
      .Orientation = xlRowField 
      .Position = 3 
     End With 
End If 
関連する問題