查找使用Excel VBA连接的位置

我有一堆Excel 2013工作簿,我必须对其进行优化,每个工作簿都有多个工作表和多个数据连接,我正在寻找快速列出的方法:

  • 连接名称
  • 连接string
  • 使用连接的位置(表格名称或范围将很有用)

我可以在连接对话框中看到所有这些信息,但是无法以编程方式跟踪它们。 我一次只想做一个文件,所以我不担心在所有的文件中运行代码,只是当我开始处理相关文件时,可以放在模块中。 到目前为止,我在这个网站上发现了这个:

Dim conn As WorkbookConnection For Each conn In ActiveWorkbook.Connections Debug.Print conn.Name Next conn 

但我找不到位置信息。 任何指针将非常感激地收到。

干杯

凯尔

预期产出:

 (Connection's Name): Sheet'sName|1st Cell of Range Connection's ODBC Command Text (Connection's Name): Sheet'sName|1st Cell of 1st Range // Sheet'sName|1st Cell of 2nd Range 

干得好 :

 Private Sub List_Connections_Ranges() Dim wC As WorkbookConnection Dim rG As Range Dim TpStr As String For Each wC In ActiveWorkbook.Connections TpStr = "(" & wC.Name & ") found on : " For Each rG In wC.Ranges TpStr = TpStr & rG.Parent.Name & "|" & rG.Cells(1, 1).Address(0, 0) & " // " Next rG Debug.Print Left(TpStr, Len(TpStr) - 4) Debug.Print wC.ODBCConnection.CommandText Next wC End Sub 

代码看起来对我好。 你可以试试这样看你是否有连接:

 Public Sub TestMePlease() Dim conn As WorkbookConnection If ActiveWorkbook.Connections.Count > 0 Then For Each conn In ActiveWorkbook.Connections Debug.Print conn.Name Next conn Else Debug.Print "No connection found" End If End Sub 

编辑:有关连接的更多信息在库中。 要看到它,请按Ctrl +空格像这样:

在这里输入图像描述

关于表名,这里是:

  Debug.Print conn.Ranges.Item(1).Parent.Name 

关于连接string,微软不会给你从VBA访问这些信息的可能性。 如果可用,它应该是一个巨大的安全问题。