首页 > 解决方案 > 更新它所基于的数据透视表和链接表的问题

问题描述

我有一个链接到经常更新的 Access 数据库的表。该表用于填充数据透视表。

我有按下刷新按钮时执行的以下宏。问题是选择此按钮时,链接表被刷新,但是要刷新枢轴表,我必须在链接的表格完成新数据刷新后再次按下按钮。

我应该等到使用 VBA 刷新链接表并在之后刷新数据透视表吗?如何才能做到这一点:

当前宏:

Sub RefreshFunc()

ThisWorkbook.RefreshAll

End Sub

标签: excel

解决方案


尝试这个...

Sub ChangeConnectionRefreshModeAndRefreshAll()

'-------------------------------------------------------------------------------------
' Purpose:   Turn off background refreshing for all workbook connections.
'            This prevents hangs and crashes.  Cycle 2x
'
' Source:    https://www.myonlinetraininghub.com/excel-forum/vba-macros/pause-macro-until-power-queries-finished-refreshing
'
' Revisions
' 01/30/19
'-------------------------------------------------------------------------------------

    Dim Connection As WorkbookConnection
    Dim bugfix As Integer

        For bugfix = 1 To 2

            On Error Resume Next

            For Each Connection In ActiveWorkbook.Connections

                With Connection

                    If (.Type = xlConnectionTypeODBC) Then
                        .ODBCConnection.BackgroundQuery = False

                    Else

                        If (.Type = xlConnectionTypeOLEDB) Then
                            .OLEDBConnection.BackgroundQuery = False

                        End If

                    End If

                End With

            Connection.Refresh

            Next Connection

        Next bugfix

End Sub

推荐阅读