vba 使用宏刷新我的 excel 工作簿中的所有数据透视表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/70947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refreshing all the pivot tables in my excel workbook with a macro
提问by Lipis
I have a workbook with 20 different pivot tables. Is there any easy way to find all the pivot tables and refresh them in VBA?
我有一个包含 20 个不同数据透视表的工作簿。有没有什么简单的方法可以找到所有数据透视表并在 VBA 中刷新它们?
回答by GSerg
Yes.
是的。
ThisWorkbook.RefreshAll
Or, if your Excel version is old enough,
或者,如果您的 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
回答by Robert Mearns
This VBA code will refresh all pivot tables/charts in the workbook.
此 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
Another non-programatic option is:
另一个非编程选项是:
- Right click on each pivot table
- Select Table options
- Tick the 'Refresh on open'option.
- Click on the OK button
- 右键单击每个数据透视表
- 选择表格选项
- 勾选“打开时刷新”选项。
- 单击确定按钮
This will refresh the pivot table each time the workbook is opened.
这将在每次打开工作簿时刷新数据透视表。
回答by Kevin
ActiveWorkbook.RefreshAllrefreshes everything, not only the pivot tables but also the ODBC queries. I have a couple of VBA queries that refer to Data connections and using this option crashes as the command runs the Data connections without the detail supplied from the VBA
ActiveWorkbook.RefreshAll刷新所有内容,不仅是数据透视表,还包括 ODBC 查询。我有几个 VBA 查询引用数据连接,并且使用此选项会崩溃,因为该命令运行数据连接而没有 VBA 提供的详细信息
I recommend the option if you only want the pivots refreshed
如果您只想刷新枢轴,我推荐该选项
Sub RefreshPivotTables()
Dim pivotTable As PivotTable
For Each pivotTable In ActiveSheet.PivotTables
pivotTable.RefreshTable
Next
End Sub
回答by Steve WahWah Weeks
In certain circumstances you might want to differentiate between a PivotTable and its PivotCache. The Cache has it's own refresh method and its own collections. So we could have refreshed all the PivotCaches instead of the PivotTables.
在某些情况下,您可能想要区分数据透视表和它的数据透视缓存。Cache 有自己的刷新方法和自己的集合。所以我们可以刷新所有数据透视缓存而不是数据透视表。
The difference? When you create a new Pivot Table you are asked if you want it based on a previous table. If you say no, this Pivot Table gets its own cache and doubles the size of the source data. If you say yes, you keep your WorkBook small, but you add to a collection of Pivot Tables that share a single cache. The entire collection gets refreshed when you refresh any single Pivot Table in that collection. You can imagine therefore what the difference might be between refreshing every cache in the WorkBook, compared to refreshing every Pivot Table in the WorkBook.
区别?当您创建新的数据透视表时,系统会询问您是否需要基于以前的表。如果您拒绝,此数据透视表将获得自己的缓存并将源数据的大小加倍。如果您同意,您的 WorkBook 将保持较小,但您将添加到共享单个缓存的数据透视表集合中。当您刷新该集合中的任何单个数据透视表时,整个集合都会刷新。因此,您可以想象刷新工作簿中的每个缓存与刷新工作簿中的每个数据透视表之间的区别。
回答by Steve WahWah Weeks
There is a refresh all option in the Pivot Table tool bar. That is enough. Dont have to do anything else.
数据透视表工具栏中有一个全部刷新选项。足够了。不必做任何其他事情。
Press ctrl+alt+F5
按 ctrl+alt+F5
回答by LohanJ
You have a PivotTablescollection on a the VB Worksheetobject. So, a quick loop like this will work:
您在 VB工作表对象上有一个数据透视表集合。因此,像这样的快速循环将起作用:
Sub RefreshPivotTables()
Dim pivotTable As PivotTable
For Each pivotTable In ActiveSheet.PivotTables
pivotTable.RefreshTable
Next
End Sub
Notes from the trenches:
战壕注意事项:
- Remember to unprotect any protected sheets before updating the PivotTable.
- Save often.
- I'll think of more and update in due course... :)
- 请记住在更新数据透视表之前取消保护任何受保护的工作表。
- 经常保存。
- 我会想到更多并在适当的时候更新... :)
Good luck!
祝你好运!
回答by user3564681
The code
编码
Private Sub Worksheet_Activate()
Dim PvtTbl As PivotTable
Cells.EntireColumn.AutoFit
For Each PvtTbl In Worksheets("Sales Details").PivotTables
PvtTbl.RefreshTable
Next
End Sub
works fine.
工作正常。
The code is used in the activate sheet module, thus it displays a flicker/glitch when the sheet is activated.
该代码用于激活工作表模块,因此当工作表被激活时它会显示闪烁/故障。
回答by Rajiv Singh
Even we can refresh particular connectionand in turn it will refresh all the pivots linked to it.
即使我们可以刷新特定的连接,反过来它也会刷新与其链接的所有枢轴。
For this code I have created slicer from table present in Excel:
对于此代码,我从 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
回答by RBhandal
I have use the command listed below in the recent past and it seems to work fine.
我最近使用过下面列出的命令,它似乎工作正常。
ActiveWorkbook.RefreshAll
Hope that helps.
希望有帮助。
回答by Karuna
If you are using MS Excel 2003 then go to view->Tool bar->Pivot Table From this tool bar we can do refresh by clicking ! this symbol.
如果您使用的是 MS Excel 2003,则转到视图->工具栏->数据透视表从此工具栏中,我们可以通过单击刷新!这个符号。

