it-swarm.cn

使用宏刷新Excel工作簿中的所有数据透视表

我有一个包含20个不同数据透视表的工作簿。有没有简单的方法来查找所有数据透视表并在VBA中刷新它们?

77
Lipis

是。

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
157
GSerg

此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

另一个非编程选项是:

  • 右键单击每个数据透视表
  • 选择表选项
  • 勾选 '打开时刷新' 选项。
  • 单击“确定”按钮

这将在每次打开工作簿时刷新数据透视表。

24
Robert Mearns

ActiveWorkbook.RefreshAll刷新所有内容,不仅包括数据透视表,还包括ODBC查询。我有几个VBA查询引用数据连接并使用此选项崩溃,因为该命令运行数据连接而没有从VBA提供的详细信息

如果您只想刷新支点,我建议使用该选项

Sub RefreshPivotTables()     
  Dim pivotTable As PivotTable     
  For Each pivotTable In ActiveSheet.PivotTables         
    pivotTable.RefreshTable     
  Next 
End Sub 
19
Kevin

在某些情况下,您可能希望区分数据透视表和其数据透视表。 Cache有自己的刷新方法和自己的集合。所以我们可以刷新所有的数据透视表而不是数据透视表。

区别?创建新的数据透视表时,系统会根据前一个表询问您是否需要它。如果您拒绝,则此数据透视表将获得自己的缓存,并使源数据的大小加倍。如果您说是,则保持WorkBook较小,但您添加到共享单个缓存的数据透视表集合。刷新该集合中的任何单个数据透视表时,将刷新整个集合。因此,与刷新WorkBook中的每个数据透视表相比,您可以想象在刷新WorkBook中的每个缓存之间可能存在的差异。

8
Steve WahWah Weeks

“数据透视表”工具栏中有一个全部刷新选项。足够了。不必做任何其他事情。

按ctrl + alt + F5

5
Prasenjit

在VB 工作表 object上有一个 PivotTables collection。所以,像这样的快速循环将起作用:

Sub RefreshPivotTables()
    Dim pivotTable As PivotTable
    For Each pivotTable In ActiveSheet.PivotTables
        pivotTable.RefreshTable
    Next
End Sub

来自战壕的笔记:

  1. 在更新数据透视表之前,请记住取消保护任何受保护的工作表。
  2. 经常保存
  3. 我会想到更多,并在适当的时候更新...... :)

祝好运!

1
LohanJ

偶数 我们可以刷新特定的连接 反过来它将刷新链接到它的所有枢轴。

对于这段代码,我从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
0
Rajiv Singh

代码

Private Sub Worksheet_Activate()
    Dim PvtTbl As PivotTable
        Cells.EntireColumn.AutoFit
        For Each PvtTbl In Worksheets("Sales Details").PivotTables
        PvtTbl.RefreshTable
        Next
End Sub 

工作良好。

该代码用于激活工作表模块,因此在激活工作表时会显示闪烁/毛刺。

0
user3564681