excelvbavba6

Update connections and queries with a button using VBA Excel Macros


I have many workbooks with many pivot tables and many connections on different sheets. What I'm trying to do is update the pivot tables and external connections separately. For this I already got a code that updates only the dynamic tables:

Dim Hoja As Worksheet
Dim TD As PivotTable

For Each Hoja In ActiveWorkbook.Sheets
    For Each TD In Hoja.PivotTables
        'Actualizar cada TD
        TD.RefreshTable
    Next TD
Next Hoja

But now I am missing a similar code that updates only the external connections massively independent of the number and location (sheets or books) of said connections.

The "Refresh All" button in Excel does not work for me because it also updates the dynamic tables.

Currently I'm handling it like this:

ActiveWorkbook.Connections("Consulta - Sheet1").Refresh
ActiveWorkbook.Connections("Consulta - Table 1").Refresh

However, I have different books with different one-to-one connections, I would have to make several buttons for each book because of the different connections that exist. My goal is that with a single button I can update the connections of any workbook just as I do with dynamic tables.


Solution

  • Solution:

    Sub UpdateConnections()
    
    Dim conn As WorkbookConnection
    
    For Each conn In ActiveWorkbook.Connections
        conn.Refresh
    Next conn
    
    End Sub