excel - 工作簿中的 VBA ADO 连接到同一工作簿
问题描述
我有一个工作簿,其中有一些 ADO 连接到同一个工作簿中的数据表,并且在建立连接时遇到问题,因为 Excel 在另一个实例中打开了我的工作簿。
字符串连接是:
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & ThisWorkbook.FullName & "';Extended Properties='Excel 12.0;HDR=YES;IMEX=1';"
有人可以帮我解决这个问题。
谢谢。
解决方案
采取以下功能
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
不应该造成任何伤害。这是为混合数据列检索数据的一种更安全的方法。
推荐阅读
- scala - java.lang.NoClassDefFoundError: scala/collection/GenTraversableOnce 在 kafka.utils.TestUtils.tempDir(TestUtils.scala)
- arrays - 通过从数组中删除除给定数组索引处的元素之外的所有元素来更新整个 json
- python-3.x - 将 Picked 或 Joblib 预训练的 ML 模型加载到 Sagemaker 并作为端点托管
- python - 将元组从熊猫导出到 sql
- python - 如何在 spacy 的文本分类中让某些类别比其他类别更重要
- css - 在primeNg p表中未选择行时如何更改颜色?
- python - 无法在 pyspark 中超过 1 小时的翻滚窗口进行分组
- javascript - 如何在对象文字中创建`this`绑定成员?
- kubernetes - kubernates 在某个端口上通过入口公开服务
- html - 我无法将页脚向右对齐