Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh All Queries in Workbook

This works for .xls books, but can it be altered for .xlsx workbooks as well? Or is their syntax that will work for both?

Option Explicit
Public Sub RefreshQueries()
  Dim wks As Worksheet
  Dim qt As QueryTable
  For Each wks In Worksheets
    For Each qt In wks.QueryTables
        qt.Refresh BackgroundQuery:=False
    Next qt
  Next wks
  Set qt = Nothing
  Set wks = Nothing
End Sub

EDIT -- So it seems my syntax does refresh .xlsx workbooks, but not queries that are from sql server. How can those be refreshed via VBA.

like image 614
Simon Paris Avatar asked Jul 17 '15 12:07

Simon Paris


People also ask

How do I refresh all queries in VBA?

You can trigger the refreshing of your stock data by either using keyboard shortcut Ctrl+Alt+F5 or navigating to your Excel Ribbon's Data tab and clicking the Refresh All button within the Queries & Connections button group.

How do you refresh queries in order?

Right click on a query and choose Properties In the Query Properties dialog you can, for example, set the query to be updated every hour. Or you can have it automatically update each time you open the file.

Can you refresh queries in Excel Online?

Select the Data tab > then choose Refresh All. Open the Queries Pane > then select Refresh.


2 Answers

First, no macro will work in a .xlsx workbook because .xlsx workbooks can't contain macros - you need to save as a macro-enabled workbook which has the extension .xlsm.

In Excel 2007 and later, user created external data connections to SQL Server data sources (amongst others) will result not in a QueryTables member, but in a ListObject which will possess a QueryTable object that can be accessed via the ListObject.QueryTable property - see Dick Kusleika's answer to this question. The following code should refresh both types of queries:

Option Explicit
Public Sub RefreshQueries()

  Dim wks As Worksheet
  Dim qt As QueryTable
  Dim lo As ListObject

  For Each wks In Worksheets
    For Each qt In wks.QueryTables
        qt.Refresh BackgroundQuery:=False
    Next qt

    For Each lo In wks.ListObjects
        lo.QueryTable.Refresh BackgroundQuery:=False
    Next lo

  Next wks

  Set qt = Nothing
  Set wks = Nothing
End Sub

I wasn't previously familiar with the ListObject type so I don't know if you can have a ListObject on a worksheet that doesn't have a QueryTable, which might cause an error in the above code - you might need to check for this.

like image 68
nekomatic Avatar answered Oct 03 '22 23:10

nekomatic


The answer from @nekomatic throws error 1004 for me (although it apparently works for others). I use this instead:

Public Sub RefreshAllQueries()
    ' Refresh all queries (tables querying data from another source).

    Dim iWorksheet As Excel.Worksheet
    Dim iTable As Excel.ListObject
    Dim iQueryTable As Excel.QueryTable

    ' Check each worksheet.
    For Each iWorksheet In Excel.ActiveWorkbook.Worksheets

        ' Check each table.
        For Each iTable In iWorksheet.ListObjects

            If iTable.SourceType = Excel.XlListObjectSourceType.xlSrcQuery Then
                ' Table is a query table.
                With iTable.QueryTable
                    .BackgroundQuery = False    ' setting to wait for query to refresh
                    .Refresh
                End With

            End If

        Next iTable


        For Each iQueryTable In iWorksheet.QueryTables
            iQueryTable.Refresh BackgroundQuery:=False  ' wait for query to refresh
        Next iQueryTable

    Next iWorksheet
End Sub
like image 28
ChrisB Avatar answered Oct 04 '22 00:10

ChrisB