Ho una cartella di lavoro con 20 diverse tabelle pivot. C'è un modo semplice per trovare tutte le tabelle pivot e aggiornarle in VBA?
Sì.
ThisWorkbook.RefreshAll
Oppure, se la tua versione di Excel è abbastanza vecchia,
Dim Sheet as WorkSheet, Pivot as PivotTable
For Each Sheet in ThisWorkbook.WorkSheets
For Each Pivot in Sheet.PivotTables
Pivot.RefreshTable
Pivot.Update
Next
Next
Questo codice VBA aggiornerà tutte le tabelle/i grafici pivot nella cartella di lavoro.
Sub RefreshAllPivotTables()
Dim PT As PivotTable
Dim WS As Worksheet
For Each WS In ThisWorkbook.Worksheets
For Each PT In WS.PivotTables
PT.RefreshTable
Next PT
Next WS
End Sub
Un'altra opzione non programmatica è:
Ciò aggiornerà la tabella pivot ogni volta che si apre la cartella di lavoro.
ActiveWorkbook.RefreshAll
aggiorna tutto, non solo le tabelle pivot ma anche le query ODBC. Ho un paio di query VBA che fanno riferimento a connessioni dati e l'utilizzo di questa opzione si arresta in modo anomalo mentre il comando esegue le connessioni dati senza i dettagli forniti dal VBA
Raccomando l'opzione se si desidera aggiornare solo i perni
Sub RefreshPivotTables()
Dim pivotTable As PivotTable
For Each pivotTable In ActiveSheet.PivotTables
pivotTable.RefreshTable
Next
End Sub
In determinate circostanze, è possibile che si desideri distinguere tra una tabella pivot e la relativa cache pivot. La cache ha il suo metodo di aggiornamento e le sue raccolte. Quindi avremmo potuto aggiornare tutte le PivotCaches anziché le tabelle pivot.
La differenza? Quando si crea una nuova tabella pivot, viene richiesto se lo si desidera in base a una tabella precedente. Se si dice di no, questa tabella pivot ottiene la propria cache e raddoppia la dimensione dei dati di origine. Se dici di sì, mantieni piccolo il tuo libro di lavoro, ma aggiungi a una raccolta di tabelle pivot che condividono una singola cache. L'intera raccolta viene aggiornata quando si aggiorna una singola tabella pivot in quella raccolta. È quindi possibile immaginare quale potrebbe essere la differenza tra l'aggiornamento di ogni cache nel Workbook, rispetto all'aggiornamento di ogni tabella pivot nel WorkBook.
Esiste un'opzione di aggiornamento di tutti nella barra degli strumenti Tabella pivot. Questo è sufficiente. Non devi fare altro.
Premi ctrl + alt + F5
È presente una raccolta tabelle pivot su un foglio di lavoro VB oggetto. Quindi, un ciclo rapido come questo funzionerà:
Sub RefreshPivotTables()
Dim pivotTable As PivotTable
For Each pivotTable In ActiveSheet.PivotTables
pivotTable.RefreshTable
Next
End Sub
Note dalle trincee:
In bocca al lupo!
Anche possiamo aggiornare una connessione particolare e a sua volta aggiornerà tutti i perni ad essa collegati.
Per questo codice ho creato lo slicer dalla tabella presente in Excel:
Sub UpdateConnection()
Dim ServerName As String
Dim ServerNameRaw As String
Dim CubeName As String
Dim CubeNameRaw As String
Dim ConnectionString As String
ServerNameRaw = ActiveWorkbook.SlicerCaches("Slicer_ServerName").VisibleSlicerItemsList(1)
ServerName = Replace(Split(ServerNameRaw, "[")(3), "]", "")
CubeNameRaw = ActiveWorkbook.SlicerCaches("Slicer_CubeName").VisibleSlicerItemsList(1)
CubeName = Replace(Split(CubeNameRaw, "[")(3), "]", "")
If CubeName = "All" Or ServerName = "All" Then
MsgBox "Please Select One Cube and Server Name", vbOKOnly, "Slicer Info"
Else
ConnectionString = GetConnectionString(ServerName, CubeName)
UpdateAllQueryTableConnections ConnectionString, CubeName
End If
End Sub
Function GetConnectionString(ServerName As String, CubeName As String)
Dim result As String
result = "OLEDB;Provider=MSOLAP.5;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=" & CubeName & ";Data Source=" & ServerName & ";MDX Compatibility=1;Safety Options=2;MDX Missing Member Mode=Error;Update Isolation Level=2"
'"OLEDB;Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=" & CubeName & ";Data Source=" & ServerName & ";Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Use Encryption for Data=False;Tag with column collation when possible=False"
GetConnectionString = result
End Function
Function GetConnectionString(ServerName As String, CubeName As String)
Dim result As String
result = "OLEDB;Provider=MSOLAP.5;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=" & CubeName & ";Data Source=" & ServerName & ";MDX Compatibility=1;Safety Options=2;MDX Missing Member Mode=Error;Update Isolation Level=2"
GetConnectionString = result
End Function
Sub UpdateAllQueryTableConnections(ConnectionString As String, CubeName As String)
Dim cn As WorkbookConnection
Dim oledbCn As OLEDBConnection
Dim Count As Integer, i As Integer
Dim DBName As String
DBName = "Initial Catalog=" + CubeName
Count = 0
For Each cn In ThisWorkbook.Connections
If cn.Name = "ThisWorkbookDataModel" Then
Exit For
End If
oTmp = Split(cn.OLEDBConnection.Connection, ";")
For i = 0 To UBound(oTmp) - 1
If InStr(1, oTmp(i), DBName, vbTextCompare) = 1 Then
Set oledbCn = cn.OLEDBConnection
oledbCn.SavePassword = True
oledbCn.Connection = ConnectionString
oledbCn.Refresh
Count = Count + 1
End If
Next
Next
If Count = 0 Then
MsgBox "Nothing to update", vbOKOnly, "Update Connection"
ElseIf Count > 0 Then
MsgBox "Update & Refresh Connection Successfully", vbOKOnly, "Update Connection"
End If
End Sub
Il codice
Private Sub Worksheet_Activate()
Dim PvtTbl As PivotTable
Cells.EntireColumn.AutoFit
For Each PvtTbl In Worksheets("Sales Details").PivotTables
PvtTbl.RefreshTable
Next
End Sub
funziona bene.
Il codice viene utilizzato nel modulo di attivazione del foglio, quindi visualizza uno sfarfallio/glitch quando il foglio è attivato.