Ich lerne gerade VBA und bin damit noch nicht ganz vertraut.
Ich möchte nur eine Pivot-Tabelle mit einem dynamischen Bereich erstellen, wenn die Pivot-Tabelle noch nicht vorhanden ist. Wenn die Tabelle bereits vorhanden ist, muss der Pivot-Vorsprung aktualisiert werden.
bisher habe ich folgendes:
Sub CreatingPivot()
Dim PCache As PivotCache, LastRow As Long, pt As PivotTable
Dim ws As Worksheet
On Error Resume Next
Set ws = Sheets("Pivot")
If Err.Number <> 0 Then
Worksheets("Sheet1").Activate
Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=1, SourceData:=Range("A1").CurrentRegion.Address)
Worksheets.Add
ActiveSheet.Name = "Pivot"
ActiveWindow.DisplayGridlines = False
Set pt = ActiveSheet.PivotTables.Add(PivotCache:=PCache, TableDestination:=Range("A1"), TableName:="PivotTable1")
Else
Sheets("Pivot").RefreshTable
End If
End Sub
Antworten:
1 für die Antwort № 1Versuchen Sie etwas wie:
Dim PivTbl as PivotTable
On Error Resume Next
Set PivTbl = Sheets("Pivot").PivotTables("PivotTable1")
On Error Goto 0
If PivTbl Is Nothing Then
"Create pivot table
End If
"Do your stuff
BEARBEITEN FÜR OP COMMENT / FOLLOW-ON Q
Aktualisieren Sie das PivotTable nicht das Arbeitsblatt
Dim PivTbl As PivotTable
Set PivTbl = Sheets("Pivot").PivotTables("PivotTable1")
PivTbl.RefreshTable