excelvbapivotoledbconnection

Excel VBA - deleting connections no longer used by any pivot table in the workbook


Here's my problem:

I have a workbook with around 25 worksheets, which all have at least one pivot table. The workbook contains a macro which can read from an input file (with several worksheets in it), can detect the range holding data and add a connection to the proper ranges. This new connection (OLEDB) then replaces the 'default' connection for the table from the template file.

So far, so good. Except it does not remove the initial connection, which remains in the workbook - just.. not being used by any of the tables. After quite extensive testing there's now 800+ connections in the workbook and well.. it's starting to really show in how slow the workbook is responding.

I'm trying to find a way to: Cycle through all connections (wb.connections(i), using i as a counter) Cycle through all worksheets and the pivottables in them, to see if any of the pivottables in the worksheet use that connection. If after cycling through all, no match has appeared, delete the connection.

I've tried (building of a ChatGPT answer..) this:

Sub OudeConnectiesVerwijderen()
    Dim wb As Workbook
    Dim conn As WorkbookConnection
    Dim pt As PivotTable
    Dim isUsed As Boolean
    Dim i As Long

    ' Set the workbook to the active workbook
    Set wb = ThisWorkbook

    ' Loop backwards through the connections to avoid index issues when deleting
    For i = wb.Connections.Count To 1 Step -1
        Set conn = wb.Connections(i)
        isUsed = False

        ' Check if the connection is used in any pivot table
        Dim ws As Worksheet
        For Each ws In wb.Worksheets
            For Each pt In ws.PivotTables
                 If pt.PivotCache.Connection = conn.Name Then
                    isUsed = True
                    Exit For
                End If
            Next pt
            If isUsed Then Exit For
        Next ws

        ' If not used, delete the connection
        If Not isUsed Then
            conn.Delete
        End If
    Next i

    
End Sub

It got my hopes up, but the issue is in this line:

                 If pt.PivotCache.Connection = conn.Name Then

One is a Connection-object, and the other is a text string.
There's never a match found, and all connections get deleted.

However, I can't seem to either:

  1. compare two 'connection' objects to get a match (by dropping the .Name) - still didn't detect a 'match'
  2. find a way to get the name for the connection a given pivottable uses to reference it with the connection name, I can only get the 'connection' object, not a name string. I (of course) cannot get a match because well - different data type to begin with. pt.PivotCache.Connection.Name is not a supported function, despite something like that likely being the solution to my problem.
  3. find any other way to find through VBA if a 'connection' (OLEDB) is used anywhere and if not delete it.
    (as in - query another property of the connection, and if that's null or some value it'd indicate me if the connection is 'no longer in use'.

I'm looking for any viable way to detect the unused connections and delete them.

Ideally I'd also want to avoid a such 'backlog' building up, by deleting the old connection whenever I overrule with a new one - but even in that case, I still can't seem to figure out how to get the name of the connection that was in place beforehand, which seems a requirement to be able to delete it..

Solution found - for now only in comments. Will accept answer as soon as posted as such - Thank you for the help


Solution

  • Pls try

    If pt.PivotCache.WorkbookConnection.Name = conn.Name Then
    

    Microsoft documentation:

    WorkbookConnection object (Excel)