VBA:使用Excel查询访问。为什么这么慢?

Dan*_*Dan 6 excel vba ado jet

我在网上找到了此代码以查询Access并将数据输入到excel(2003)中,但是它比它应该慢得多:

Sub DataPull(SQLQuery, CellPaste)
Dim Con As New ADODB.Connection
Dim RST As New ADODB.Recordset
Dim DBlocation As String, DBName As String
Dim ContractingQuery As String

If SQLQuery = "" Then

Else
    DBName = Range("DBName")
    If Right(DBName, 4) <> ".mdb" Then DBName = DBName + ".mdb"

    DBlocation = ActiveWorkbook.Path
    If Right(DBlocation, 1) <> "\" Then DBlocation = DBlocation + "\"

    Con.ConnectionString = DBlocation + DBName
    Con.Provider = "Microsoft.Jet.OLEDB.4.0"
    Con.Open

    Set RST = Con.Execute(SQLQuery)
    Range(CellPaste).CopyFromRecordset RST

    Con.Close
End If

End Sub
Run Code Online (Sandbox Code Playgroud)

问题是此代码需要很长时间。如果我打开Access并在其中运行查询,则大约需要1/10的时间。反正有加速的吗?还是这可能需要很长时间?我所有的查询都是简单的select查询,带有简单的where语句,没有连接。甚至select * from [test]查询所花的时间也比它要长得多。

编辑:我应该指定该行

Range(CellPaste).CopyFromRecordset RST

是花了很长时间的那个。

Dan*_*Dan 1

许多公式可能会引用该查询。尝试暂时在宏中打开手动计算,并在所有查询完成更新后将其关闭。

这应该会加快速度,但仍然不能解决根本问题。