2017-05-01 30 views
1

Excel VBAの別のテーブルからピボットテーブルを作成するマクロを記録しました。私は、なぜ私はこのエラーになってるかわからない:「ファイル名を指定して実行時エラー 『5』」無効なプロシージャ呼び出しまたは引数「VBAでピボットテーブルを作成する際のプロシージャコールまたは引数のエラーが無効です

誰かが私が間違ってやっている何を教えてもらえます

Sheets("Sheet1").Select 
Range("A1").Select 
Range(Selection, Selection.End(xlToRight)).Select 
Range(Selection, Selection.End(xlDown)).Select 

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ 
    "Table_FTE_Distributions4.24", Version:=xlPivotTableVersion15).CreatePivotTable TableDestination:= _ 
    "'Sheet6'! R3C1", TableName:="PivotTable1", DefaultVersion:=15 
+1

ピボットテーブルを作成した後は、他のピボットテーブルもピボットテーブル1という名前にすることはできません。それに応じて名前を変更する必要があります。たぶんポジションもあるかもしれません。しかし、私はポジションがエラーを引き起こすことはないと思うが、警告を引き起こすだろう。 – Masoud

+0

^また、「Table_FTE_Distributions4.24」とは何ですか? 'Named Range'? 'テーブル'( 'ListObject')? –

+0

これは私がピボットを外すテーブルの名前です。 – Michaela

答えて

0

。? it'aが既に作成している場合

。ちょうど更新PivotCacheで更新(過去のコードから実行されます)。それが存在しない場合には「Sheet6」に「PivotTable1」を作成するために、以下のコードを試してみてくださいコード

Option Explicit 

Sub RefreshPivots() 

Dim ShtPivot As Worksheet 
Dim PivTbl  As PivotTable 
Dim PivCache As PivotCache 
Dim Tbl   As ListObject 
Dim SrcRng  As Range 

' set the Pivot Sheet 
Set ShtPivot = Worksheets("Sheet6") 

' set the ListObject to "Table_FTE_Distributions4.24" 
Set Tbl = Worksheets("Sheet1").ListObjects("Table_FTE_Distributions4.24") 

' set the Pivot Caches SourceData 
Set SrcRng = Tbl.Range 

' set the Pivot Cache 
Set PivCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SrcRng) 

' add this line in case the Pivot table doesn't exit >> first time running this Macro 
On Error Resume Next 
Set PivTbl = ShtPivot.PivotTables("PivotTable1") ' check if "PivotTable1" Pivot Table already created (in past runs of this Macro) 

On Error GoTo 0 
If PivTbl Is Nothing Then 
    ' create a new Pivot Table in "Sheet6", start from Cell A3 
    Set PivTbl = ShtPivot.PivotTables.Add(PivotCache:=PivCache, TableDestination:=ShtPivot.Range("A3"), TableName:="PivotTable1") 

Else 
    ' just refresh the Pivot cache with the updated Range in "Table_FTE_Distributions4.24" 
    MsgBox "PivotTable1 already exists, onyl need to refresh the Pivot Cache" 
    PivTbl.ChangePivotCache PivCache 
    PivTbl.RefreshTable 
End If 

End Sub 
関連する問題