我有一个包含 20 个不同数据透视表的工作簿。有什么简单的方法可以找到所有数据透视表并在 VBA 中刷新它们?
10 回答
是的。
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
此 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
另一个非编程选项是:
- 右键单击每个数据透视表
- 选择表格选项
- 勾选“打开时刷新”选项。
- 点击确定按钮
这将在每次打开工作簿时刷新数据透视表。
ActiveWorkbook.RefreshAll
刷新一切,不仅是数据透视表,还有 ODBC 查询。我有几个引用数据连接的 VBA 查询,使用此选项会崩溃,因为命令运行数据连接时没有 VBA 提供的详细信息
如果您只想刷新枢轴,我推荐该选项
Sub RefreshPivotTables()
Dim pivotTable As PivotTable
For Each pivotTable In ActiveSheet.PivotTables
pivotTable.RefreshTable
Next
End Sub
在某些情况下,您可能希望区分数据透视表和它的数据透视缓存。Cache 有自己的刷新方法和自己的集合。因此,我们可以刷新所有数据透视缓存而不是数据透视表。
区别?当您创建一个新的数据透视表时,系统会询问您是否希望它基于以前的表。如果您拒绝,则此数据透视表将获得自己的缓存并将源数据的大小翻倍。如果您同意,您的 WorkBook 将保持较小,但您会添加到共享单个缓存的数据透视表集合中。当您刷新该集合中的任何单个数据透视表时,整个集合都会刷新。因此,您可以想象刷新 WorkBook 中的每个缓存与刷新 WorkBook 中的每个数据透视表之间可能有什么区别。
数据透视表工具栏中有一个全部刷新选项。足够了。不必做任何其他事情。
按 ctrl+alt+F5
您在 VB工作表对象上有一个数据透视表集合。因此,像这样的快速循环将起作用:
Sub RefreshPivotTables()
Dim pivotTable As PivotTable
For Each pivotTable In ActiveSheet.PivotTables
pivotTable.RefreshTable
Next
End Sub
战壕笔记:
- 请记住在更新数据透视表之前取消保护任何受保护的工作表。
- 经常保存。
- 我会考虑更多并在适当的时候更新...... :)
祝你好运!
编码
Private Sub Worksheet_Activate()
Dim PvtTbl As PivotTable
Cells.EntireColumn.AutoFit
For Each PvtTbl In Worksheets("Sales Details").PivotTables
PvtTbl.RefreshTable
Next
End Sub
工作正常。
该代码用于激活工作表模块,因此在激活工作表时会显示闪烁/故障。
甚至我们可以刷新特定的连接,反过来它会刷新所有链接到它的枢轴。
对于此代码,我从 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
我最近使用过下面列出的命令,它似乎工作正常。
ActiveWorkbook.RefreshAll
希望有帮助。
如果您使用的是 MS Excel 2003,请转到查看->工具栏->数据透视表 在此工具栏中,我们可以通过单击来进行刷新!这个符号。