如何在vba中使用ADODB连接


  1. 我需要按数组的数据
  2. 目标文件在网络上,数据值在10秒内增加
  3. 需要连接到此文件(.xls(并制作实时图表

现在的问题是如何将ADODB用于这个打开的文件

发生2例:

第一:

dim path as string, DB as string
dim cnt as ADODB.Connection
OLEDB = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & path & DB & "; 
extended properties=""excel 12.0 xmlHDR=YES"";"
Set cnt = New ADODB.Connection
cnt.Open OLEDB

然后我得到了";找不到ISAM安装";错误

第二:

dim path as string, DB as string
dim cnt as ADODB.Connection
OLEDB = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & path & DB &"; 
extended properties=""excel 8.0;HDR=YES;IMEX=1;';"""
Set cnt = New ADODB.Connection
cnt.Open OLEDB

然后我得到了3706 runtime error, cant find supporter

那么我怎么了?

这就是我通常运行它的方式:

DbConn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & path & ";" & _
"Extended Properties=""Excel 12.0; HDR=YES"";"

其中path是完整的文件路径+文件名。你可以在这里找到更多关于连接字符串的信息

然后进入SQL语句中的工作表名称和范围。例如:

sqlWhere = sheetName & "$A1:$Z1000"
DbRecSet.Open "SELECT * FROM [" & sqlWhere & "]", DbConn, adOpenStatic

编辑:

如果我理解正确的话,您有一个每10秒更改一次值的单元格,并且您希望通过ADODB获取该值。

首先,在这种情况下,您可以从连接字符串中废弃HDR=YES

假设这个细胞是A1

sqlWhere = sheetName & "$A1"
DbRecSet.Open "SELECT * FROM [" & sqlWhere & "]", DbConn, adOpenStatic
returnValue = DbRecSet.Fields(0)

path=test_fdr&quot"amp;today_total&";。xls";

OLEDB = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & path & 
";Extended Properties=""Excel 8.0;HDR=YES"";"
Set cnt = New ADODB.Connection
cnt.Open OLEDB
Rcdset.Open Source:="[" & TestSheetName & "$]", _
ActiveConnection:=cnt, _
CursorType:=adOpenStatic, _
LockType:=adLockReadOnly, _
Options:=adCmdTable
With Sheets(3).QueryTables.Add(Connection:=Rcdset, Destination:=Target_rng)

最新更新