Query Tables (QueryTables) in Excel 2010 with VBA with VBA creating many connections

前端 未结 8 711
北荒
北荒 2021-02-06 12:49

I\'m following code I found on another site. Here\'s the basics of my code:

Dim SQL As String
Dim connString As String

connString = \"ODBC;DSN=DB01;UID=;PWD=;Da         


        
8条回答
  •  生来不讨喜
    2021-02-06 13:40

    I've found that by default new connections created this way are called "Connection". What I am using is this snippet of code to remove the connection but retain the listobject.

    Application.DisplayAlerts = False
    ActiveWorkbook.Connections("Connection").Delete
    Application.DisplayAlerts = True
    

    It can easily be modified to remove the latest added connection (or if you keep track of the connections by their index).

    Application.DisplayAlerts = False
    ActiveWorkbook.Connections(ActiveWorkbook.Connections.Count).Delete
    Application.DisplayAlerts = True
    

提交回复
热议问题