【发布时间】:2014-08-19 18:22:21
【问题描述】:
我正在尝试使用 VBA 查询电子表格,但似乎遇到了 65536 行的硬限制(尽管我运行的是 Excel 2013)。
当尝试选择行数大于 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
我也试过连接字符串:
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 作为数据库 停止。
-
我喜欢危险的生活。