2017-08-29 1 views
0

Ich muss Pivot-Tabelle erstellen, aber ich möchte Zeilenfeld hinzufügen, aber ich erhalte Fehler als "Laufzeitfehler '1004' unfähig, die Pivot-Tabelle Eigenschaften des Arbeitsblatt Klasse zu erhalten“Laufzeitfehler '1004' Kann die Pivot-Tabelleneigenschaften der Arbeitsblattklasse nicht abrufen

On Error Resume Next 
    For Each WS In ActiveWorkbook.Worksheets 
     For Each PT In WS.PivotTables 
      WS.Range(PT.TableRange2.Address).Delete Shift:=xlUp 
     Next PT 
    Next WS 
Set Pvt = ActiveSheet.PivotTables("ZFIGLABACUS") 
ActiveCell.SpecialCells(xlLastCell).Select 
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select 
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ 
     "FBL5N!R1C1:R30000C14", Version:=xlPivotTableVersion14).CreatePivotTable _ 
     TableDestination:="SUMMARY!R3C9", TableName:="ZFIGLABACUS", DefaultVersion _ 
     :=xlPivotTableVersion14 
    Sheets("SUMMARY").Select 


    Pvt.PivotFields("G/L Account").Orientation = xlRowField 

ich bin nicht in der Lage, die Zeilenfelder zur Schwenk hinzuzufügen, wo ich falsch helfen Sie mir

immer am
+0

Sie löschen alle Pivot-Tabellen aus allen Arbeitsblättern. Wenn Sie die Pivot-Tabelle neu erstellen, verwenden Sie nicht 'Set Pvt = ActiveWorkbook.PivotCaches.Create ...', damit Sie eine Fehlermeldung erhalten Linie –

+0

Also was kann ich jetzt tun, um den Fehler zu beseitigen, sollte ich den bestimmten vorherigen Pivot oder etwas anderes löschen müssen. – Nithin

+0

Was versuchen Sie zu erreichen? Sie löschen alle Pivot-Tabellen und erstellen sie dann neu, warum? –

Antwort

0

den Code unten versuchen, innerhalb Erklärungen die Kommentare des Codes:

Option Explicit 

Sub AutoPivotTable() 

Dim Sht As Worksheet 
Dim PvtSht As Worksheet 
Dim SrcData As Range 
Dim PvtCache As PivotCache 
Dim PvtTbl As PivotTable 

'-- Determine the data range you want to pivot -- 
' Set the Worksheet object 
Set Sht = ThisWorkbook.Worksheets("FBL5N") 
Set SrcData = Sht.Range("A1:M30000") 

' Set the Pivot Cache from Source Data 
Set PvtCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SrcData.Address(False, False, xlA1, xlExternal)) 

' Set the Worksheet object where the Pivot Table will be placed 
Set PvtSht = ThisWorkbook.Worksheets("SUMMARY") 

On Error Resume Next 
'Set the pivot table object 
Set PvtTbl = PvtSht.PivotTables("ZFIGLABACUS") ' check if Pivot Table already created (in past runs of this Macro) 
On Error GoTo 0 
If PvtTbl Is Nothing Then '<-- Pivot Table not created >> create it 
    ' create a new Pivot Table in "SUMMARY" sheet 
    Set PvtTbl = PvtSht.PivotTables.Add(PivotCache:=PvtCache, TableDestination:=PvtSht.Range("I3"), TableName:="ZFIGLABACUS") 
    With PvtTbl 
     With .PivotFields("G/L Account") 
      .Orientation = xlRowField 
      .Position = 1 
     End With 
    End With 

Else ' just refresh the Pivot cache with the updated Range 
    PvtTbl.ChangePivotCache PvtCache 
    PvtTbl.RefreshTable 
End If 

End Sub 
Verwandte Themen