我有一个包含20个不同数据透视表的工作簿。有没有简单的方法可以找到所有数据透视表并在VBA中刷新它们?
答案 0 :(得分:157)
是
ThisWorkbook.RefreshAll
或者,如果您的Excel版本足够老,
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
答案 1 :(得分:24)
此VBA代码将刷新工作簿中的所有数据透视表/图表。
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
另一个非编程选项是:
每次打开工作簿时,都会刷新数据透视表。
答案 2 :(得分:19)
ActiveWorkbook.RefreshAll
刷新所有内容,不仅包括数据透视表,还包括ODBC查询。我有几个VBA查询引用数据连接并使用此选项崩溃,因为该命令运行数据连接而没有从VBA提供的详细信息
如果您只想刷新支点
,我建议使用此选项Sub RefreshPivotTables()
Dim pivotTable As PivotTable
For Each pivotTable In ActiveSheet.PivotTables
pivotTable.RefreshTable
Next
End Sub
答案 3 :(得分:8)
在某些情况下,您可能希望区分数据透视表和数据透视表。 Cache有自己的刷新方法和自己的集合。所以我们可以刷新所有的数据透视表而不是数据透视表。
区别?创建新的数据透视表时,系统会根据前一个表询问您是否需要它。如果您拒绝,则此数据透视表将获得自己的缓存,并使源数据的大小加倍。如果您说是,则保持WorkBook较小,但您添加到共享单个缓存的数据透视表集合。刷新该集合中的任何单个数据透视表时,将刷新整个集合。因此,您可以想象,与刷新工作簿中的每个数据透视表相比,刷新工作簿中的每个缓存之间可能存在差异。
答案 4 :(得分:5)
“数据透视表”工具栏中有一个全部刷新选项。足够了。不必做任何其他事情。
按ctrl + alt + F5
答案 5 :(得分:1)
您在VB 工作表对象上有一个数据透视表集合。因此,像这样的快速循环将起作用:
Sub RefreshPivotTables()
Dim pivotTable As PivotTable
For Each pivotTable In ActiveSheet.PivotTables
pivotTable.RefreshTable
Next
End Sub
来自战壕的笔记:
答案 6 :(得分:0)
代码
Private Sub Worksheet_Activate()
Dim PvtTbl As PivotTable
Cells.EntireColumn.AutoFit
For Each PvtTbl In Worksheets("Sales Details").PivotTables
PvtTbl.RefreshTable
Next
End Sub
工作正常。
代码用于激活工作表模块,因此在激活工作表时会显示闪烁/毛刺。
答案 7 :(得分:0)
即使我们也可以刷新特定的连接,反过来它会刷新链接到它的所有枢轴。
对于此代码,我从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
答案 8 :(得分:-1)
我最近使用了下面列出的命令,似乎工作正常。
ActiveWorkbook.RefreshAll
希望有所帮助。
答案 9 :(得分:-2)
如果您使用的是MS Excel 2003,请转到view->工具栏 - >数据透视表从这个工具栏我们可以点击刷新!这个符号。