“The workbook has reached the maximum number of connections.”

This alert indicates that your Excel workbook has reached the internal ceiling for data connection objects (Workbook.Connections) or exhausted the operating system’s connection pool. This issue commonly occurs in long-standing workbooks that have accumulated orphaned connection strings, workbooks where sheets containing query tables were repeatedly copied, or automated files where macros create new connections without deleting existing ones.

Quick Answer

Open Data > Queries & Connections, switch to the Connections tab, select unused or duplicate connections, and click the Delete button. If the error is generated by an automated macro, modify the code to delete temporary QueryTable or WorkbookConnection objects immediately after refreshing.


What Causes Connection Object Bloat

Every time you connect Excel to an external source, such as a SQL server, a web API, a text file, or an OLAP cube, Excel registers a connection object inside the workbook’s internal XML schema (xl/connections.xml).

Excel does not automatically delete connection definitions when you delete the worksheet cells that displayed the data. Over time, three scenarios cause workbooks to hit internal limits:

  1. Duplicating Worksheets: Copying a worksheet that contains a Power Query table or an external data range duplicates the underlying connection object, appending numbers like Query - Sales_1, Query - Sales_2, etc.
  2. VBA Script Leaks: Macros that utilize ActiveWorkbook.Connections.Add or QueryTables.Add inside loops create a new connection instance on every run. If the macro does not explicitly run .Delete on the connection object, hundreds of hidden connections accumulate.
  3. Legacy Data Range Ghosts: Legacy web queries or MS Query objects remain registered in the background even after users clear the sheet contents.

Step-by-Step Diagnostic and Cleanup Procedure

Step 1: Audit and Remove Connections via the User Interface

  1. Open the affected workbook.
  2. Navigate to the Data tab on the Ribbon.
  3. Click Queries & Connections to display the side pane.
  4. If you are using an older Excel interface, click Connections in the Queries & Connections group.
  5. Review the list of connections. Look for:
    • Repeated names with numerical suffixes (e.g., Connection1, Connection2).
    • Connections pointing to servers, file paths, or folders that no longer exist.
  6. Select an obsolete connection and click Remove (or right-click and choose Delete).
  7. Repeat this process until only the active, required connections remain.
  8. Save, close, and reopen the workbook to release cached connection handles.
┌────────────────────────────────────────────────────────┐
│  Workbook Connections                                  │
├────────────────────────────────────────────────────────┤
│  Name                         Location                 │
│  ────────────────────────────────────────────────────  │
│  Monthly_Sales                Sheet1!$A$1              │
│  Monthly_Sales_1  [ORPHAN]    None ◄────────────────── │
│  Monthly_Sales_2  [ORPHAN]    None ◄── Delete These    │
│  Legacy_ODBC_Feed [ORPHAN]    None ◄────────────────── │
│                                                        │
│  [ Add... ]      [ Remove ] ◄─ Click to Purge          │
└────────────────────────────────────────────────────────┘

Step 2: Purge Orphaned Connections Using VBA

If your workbook contains dozens of duplicate connections, removing them manually is inefficient. You can execute a simple macro in the Immediate Window to clear all unlinked connection objects.

  1. Press Alt + F11 to open the Visual Basic Editor.
  2. Press Ctrl + G to display the Immediate Window at the bottom.
  3. Paste the following one-line command and press Enter: For i = ActiveWorkbook.Connections.Count To 1 Step -1: ActiveWorkbook.Connections(i).Delete: Next i
  4. This command purges connection definitions. (Note: Standard Power Query entries that are actively linked to loaded tables will automatically regenerate their core connection definition upon the next edit or refresh).

If you want to remove only connections that are not tied to any active worksheet tables, paste and run this procedure:

Sub PurgeOrphanedConnections()
    Dim conn As WorkbookConnection
    Dim i As Long

    On Error Resume Next
    For i = ActiveWorkbook.Connections.Count To 1 Step -1
        Set conn = ActiveWorkbook.Connections(i)
        ' Check if the connection has an associated query table or model
        If conn.Type = xlConnectionTypeODBC Or conn.Type = xlConnectionTypeOLEDB Then
            If conn.Ranges.Count = 0 Then
                conn.Delete
            End If
        End If
    Next i
    On Error GoTo 0
End Sub

Step 3: Prevent Macro Leaks

If this error appears after running an automated reporting script, inspect your macro for how it handles external data:

  • The Antipattern: Using QueryTables.Add inside a daily loop. Each iteration adds a connection object that persists indefinitely.
  • The Proper Pattern: Define the connection once, then simply call .Refresh on the existing query table. If a dynamic connection is required, delete it at the end of the script:
' Correct Pattern: Purge dynamic connection upon completion
Dim qt As QueryTable
Set qt = ws.QueryTables.Add(Connection:=connString, Destination:=ws.Range("A1"))
qt.Refresh BackgroundQuery:=False
qt.Delete ' Removes the QueryTable wrapper
ActiveWorkbook.Connections(ActiveWorkbook.Connections.Count).Delete ' Clears connection

Deep File Repair: XML Extraction Method

In rare situations involving file corruption, Excel may refuse to delete connections through the interface, continuing to display the maximum connection error on startup.

  1. Save the file and change its extension from .xlsx to .zip.
  2. Extract the contents of the ZIP folder.
  3. Open the xl folder and locate connections.xml.
  4. Open connections.xml in a text editor (such as Notepad or VS Code).
  5. Inspect the file. If it contains thousands of lines of legacy <connection> tags, delete the redundant blocks or clear the file while preserving the root XML structure.
  6. Re-zip the files, restore the .xlsx extension, and launch the file.

For the full step-by-step procedure for safely editing workbook components, see How to recover a “Corrupt” Excel file using the XML extraction method. If connection drops cause background refresh failures, review Fixing “The background refresh of ‘Query’ failed.”