使用vba刷新查询

时间:2015-08-04 20:48:47

标签: excel vba excel-vba

我有一个包含5个Access查询的工作表,我已将其放入Excel中。我需要制作一个VBA来遍历5个表,并在每次运行时刷新它们。但是,我的代码似乎没有执行,它只是跳过循环。有人能指出我正确的方向。谢谢!

Private Sub CommandButton1_Click()
    Application.ScreenUpdating = False

    UnlockSheets

    ' Update the raw data queries for needed information
    Worksheets("Data").Activate
        Range("A:AR").EntireColumn.Hidden = False

        Dim QT As QueryTables
        Dim ws As Worksheet
        Set ws = Worksheets("Data")

        For Each QT In ws.QueryTables
            QT.Refresh
        Next QT
        Application.StatusBar = False

        Range("A:AR").EntireColumn.Hidden = True

    ' Refresh the pivot tables in the worksheet "pivot"
    Worksheets("Pivot").Activate
        Dim pvtTbl As PivotTable

        For Each pvtTbl In ActiveSheet.PivotTables
            pvtTbl.RefreshTable
        Next

    ' Update the rank sheet with the needed information
    Worksheets("Rank").Activate
        ActiveSheet.Calculate
        ActiveSheet.EnableCalculations = True
        DoEvents

    LockSheets

    Application.ScreenUpdating = True

End Sub

1 个答案:

答案 0 :(得分:1)

所以我想通了。我没有在所有的QueryTable中循环,而是通过添加:

进行了手动修复
ActiveWorkbook.Worksheets("Data").Range("Table_Report_Database.accdb_2[[#Headers],[Date]]").ListObject.QueryTable.Refresh BackgroundQuery:=False