我有一个工作簿,其中有一些ADO连接到同一工作簿中的数据表,并且在建立连接时遇到了问题,因为Excel在另一个实例中打开了我的工作簿。
字符串连接为:
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & ThisWorkbook.FullName & "';Extended Properties='Excel 12.0;HDR=YES;IMEX=1';"
有人可以帮我这个忙。
谢谢。
答案 0 :(得分:0)
执行以下功能
Function ConnectToXL(ByVal fileName As String)
Dim conn As New ADODB.Connection
If Dir(fileName) = "" Then
MsgBox "Could not find file " & fileName
Exit Function
End If
Dim connectionString As String
' https://www.connectionstrings.com/access/
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" _
& fileName & ";Extended Properties=""Excel 12.0;HDR=YES;"";"
conn.Open connectionString
Set ConnectToXL = conn
End Function
这是一段如何使用它的代码示例
Dim xlCon As ADODB.Connection
' Connect to the workbook
Set xlCon = ConnectToXL(ThisWorkbook.FullName)
是的,连接字符串与OP中发布的字符串没有太大不同。 IMEX=1
不应造成任何伤害。这是一种检索混合数据列数据的更安全的方法。