Excel作为数据库 – 查询超过65536行?

我试图查询使用VBA的电子表格,并运行在似乎65536行的硬限制(尽pipe我正在运行Excel 2013)。 基本上,当试图select行数大于65536的所有行时,我收到以下错误消息:

运行时错误“-2147217865(80040e37)”:

Microsoft Access数据库引擎找不到对象'Sheet1 $ A1:A65537'…..

我的代码如下:

Option Explicit Sub ExcelQuery() Dim conXLS As ADODB.Connection Dim rsXLS As ADODB.Recordset Dim strPath As String Dim strSQL As String Dim i As Integer 'Get the full directory + file name location of the current workbook (so it can query itself)' strPath = Application.ActiveWorkbook.FullName 'create the ADO connection to the excel file' Set conXLS = New ADODB.Connection With conXLS .Provider = "Microsoft.ACE.OLEDB.12.0" .ConnectionString = "Data Source=" & strPath & ";" & _ "Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1;Readonly=False""" End With conXLS.Open strSQL = "" & _ "SELECT " & _ "* " & _ "FROM " & _ "[Sheet1$A1:A65537] " 'create ADO recordset to hold contents of target sheet.' Set rsXLS = New ADODB.Recordset With rsXLS .CursorLocation = adUseClient .CursorType = adOpenForwardOnly .LockType = adLockReadOnly End With 'using SQL return contents of the target sheet' rsXLS.Open strSQL, conXLS 'disconnect the active connection' Set rsXLS.ActiveConnection = Nothing 'Return results to excel' Sheets("Sheet2").Cells(1, 1).CopyFromRecordset rsXLS Set rsXLS = Nothing 'destroy the connection object' conXLS.Close Set conXLS = Nothing End Sub 

我也尝试了连接string:

 With conXLS .Provider = "Microsoft.Jet.OLEDB.12.0" .ConnectionString = "Data Source=" & strPath & ";" & _ "Extended Properties=Excel 12.0;" .Open 

我已设置“Microsoft ActiveX数据对象6.0库”和“OLE自动化”的引用。

有趣的是,使用MSQuery似乎没有问题。

有没有人遇到过这个问题?

较早的Excel版本(2007年以前)确实有每个工作表约65k +行的限制。 运行你的代码,并引用任何对象库启动W / Excel 2007及以上(每个工作表最多1,048,576行,Lib版本相应地12.x和以上)。 与您的情况相关,请尝试使用记号[Sheet1$A:A]而不是[Sheet1$A1:A65537]