Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1268to1272
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

Pivot refresh

Pivot refresh
Albert
Guten Morgen ihr Exceljünger und Leidensgenossen...
kleiner Spaß am Rande. :)
Ich verwende seit Monaten diesen Code zum Aktualisieren von Pivot-Tabellen.
Sub Stapler_aktualisieren()
' Stapler_aktualisieren Makro
Range("A21").Select
ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
Dim ws                  As Worksheet
Dim pt                  As PivotTable
Dim pf                  As PivotField
Dim pi                  As PivotItem
On Error Resume Next
If Val(Application.Version) > 9 Then
For Each ws In ActiveWorkbook.Worksheets
For Each PivotTable In ws.PivotTables
PivotTable.ManualUpdate = True
PivotTable.RefreshTable
PivotTable.PivotCache.MissingItemsLimit = xlMissingItemsNone
PivotTable.ManualUpdate = False
Next PivotTable
Next ws
Else
For Each ws In ActiveWorkbook.Worksheets
For Each PivotTable In ws.PivotTables
PivotTable.ManualUpdate = True
PivotTable.RefreshTable
For Each PivotField In PivotTable.PivotFields
For Each PivotItem In PivotField.PivotItems
If PivotItem.RecordCount = 0 And _
Not PivotItem.IsCalculated Then
PivotItem.Delete
End If
Next PivotItem
Next PivotField
PivotTable.ManualUpdate = False
Next PivotTable
Next ws
End If
End Sub
Heute kam dann ein Kollege, der eigentlich den Code verwendet, aber jetzt verschiedene Laufzeitfehler (1004 und 438) erhält. Nach einigem googeln hab ich jetzt immer noch nicht das richtige gefunden und wende mich an euch.
Komisch ist, dass der Code bei mir ordnungsgemäß durchläuft. Es handelt sich bei der Datei um eine, die auf einem Server gespeichert ist. Hat die Servergeschwindigkeit vielleicht auch etwas damit zu tun? Denn die ist heute wieder ordentlich langsam.
Dank derweil im Voraus.
Albert

9
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Benutzer
Anzeige
AW: Pivot refresh
04.07.2012 08:56:10
Luschi
Hallo Albert,
PivotTable, PivotField und PivotItem sind von Excel-Vba benutzte Objekte und sollten nicht als Laufvariablen in For-Schleifen benutzt werden.
statt:
For Each PivotTable In ws.PivotTables
For Each PivotField In PivotTable.PivotFields
For Each PivotItem In PivotField.PivotItems
so:
For Each pt In ws.PivotTables
For Each pf In PivotTable.PivotFields
For Each pi In PivotField.PivotItems
und in den Schleifen die Variablen entsprechen anpassen!
Zum Schluß alle selbstdefinierten Objektvariablen deaktivieren.
Set ws = Nothing
Set pt = Nothing
Set pf = Nothing
Set pi = Nothing
Gruß von Luschi
aus klein-Paris
Anzeige
AW: Pivot refresh
04.07.2012 09:37:53
Albert
Hallo Luschi,
ich habs versucht...
Sub Stapler1_aktualisieren()
' Stapler_aktualisieren Makro
Range("A21").Select
ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
Dim ws As Worksheet
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
On Error Resume Next
If Val(Application.Version) > 9 Then
For Each ws In ActiveWorkbook.Worksheets
For Each pt In ws.PivotTables
PivotTable.ManualUpdate = True
PivotTable.RefreshTable
PivotTable.PivotCache.MissingItemsLimit = xlMissingItemsNone
PivotTable.ManualUpdate = False
Next pt
Next ws
Else
For Each pt In ActiveWorkbook.Worksheets
          For Each pt In ws.PivotTables
PivotTable.ManualUpdate = True
PivotTable.RefreshTable
For Each pf In PivotTable.PivotFields
For Each pf In PivotField.PivotItems
If PivotItem.RecordCount = 0 And _
Not PivotItem.IsCalculated Then
PivotItem.Delete
End If
Next pi
Next pf
PivotTable.ManualUpdate = False
Next PivotTable
Next ws
Set ws = Nothing
Set pt = Nothing
Set pf = Nothing
Set pi = Nothing
End If
End Sub
Das Makro bleibt dann in der fettmarkierten Zeile stehen.
Was ist nun das Problem?
LG
Albert
Anzeige
AW: Pivot refresh
04.07.2012 09:40:20
Hajo_Zi
fehlt da nicht next pt?
Du hast 2 x For mit der gleichen Variable.
Gruß Hajo
AW: Pivot refresh
04.07.2012 09:43:58
Albert
Hi Hajo,
sorry, kann dir nicht folgen!
LG
Albert
AW: Pivot refresh
04.07.2012 09:53:49
Hajo_Zi
Hallo Albert,
Du hast 3x For Each pt aber nur 1x Next PT
Gruß Hajo
AW: Pivot refresh
04.07.2012 10:08:02
Albert
Hi Hajo,
an dem lags nicht!
Albert
AW: Pivot refresh
04.07.2012 11:45:41
fcs
Hallo Albert,
du hast in mehreren For-Next-Schleifen die falschen Variablen eingesetz.
Nach meiner Einschätzung müßte es so aussehen:
        For Each ws In ActiveWorkbook.Worksheets
For Each pt In ws.PivotTables
PivotTable.ManualUpdate = True
PivotTable.RefreshTable
For Each pf In PivotTable.PivotFields
For Each Pi In PivotField.PivotItems
If PivotItem.RecordCount = 0 And _
Not PivotItem.IsCalculated Then
PivotItem.Delete
End If
Next Pi
Next pf
PivotTable.ManualUpdate = False
Next pt
Next ws

Gruß
Franz
Anzeige
AW: Pivot refresh
04.07.2012 10:43:13
guentherh
Also erst mal:
VBA bleibt nicht einfach stehen, da gibts eine Fehlermeldung.
Hier mal das korrigiert, was gleich ersichtlich ist
Sub Stapler1_aktualisieren()
' Stapler_aktualisieren Makro
Range("A21").Select
ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
Dim ws As Worksheet
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
On Error Resume Next
If Val(Application.Version) > 9 Then
For Each ws In ActiveWorkbook.Worksheets
For Each pt In ws.PivotTables
pt.ManualUpdate = True
pt.RefreshTable
 pt.PivotCache.MissingItemsLimit = xlMissingItemsNone
pt.ManualUpdate = False
Next pt
Next ws
Else
For Each ws In ActiveWorkbook.Worksheets
For Each pt In ws.PivotTables
pt.ManualUpdate = True
pt.RefreshTable
For Each pf In pt.PivotFields
For Each pi In pf.PivotItems
If pi.RecordCount = 0 And _
Not pi.IsCalculated Then
pi.Delete
End If
Next pi
Next pf
PivotTable.ManualUpdate = False
Next PivotTable
Next ws
Set ws = Nothing
Set pt = Nothing
Set pf = Nothing
Set pi = Nothing
End If
End Sub

Anzeige
AW: Pivot refresh
04.07.2012 12:48:14
Albert
Dankeschön guentherh,
war wohl doch das berühmte Hängerchen.
Merci
Albert

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige