I am trying to perform an ADODB query on a named range in an Excel 2013 workbook.
My code is as follows:
Option Explicit
Sub SQL_Extract()
Dim objConnection As ADODB.Connection
Dim objRecordset As ADODB.Recordset
Set objConnection = CreateObject("ADODB.Connection") ' dataset query object
Set objRecordset = CreateObject("ADODB.Recordset") ' new dataset created by the query
objConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & ThisWorkbook.FullName & ";" & _
"Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"";"
objConnection.Open
objRecordset.Open "SELECT * FROM [HighRange]", objConnection, adOpenStatic, adLockOptimistic, adCmdText
If Not objRecordset.EOF Then
ActiveSheet.Cells(1, 1).CopyFromRecordset objRecordset
End If
objRecordset.Close
objConnection.Close
End Sub
If the range HighRange
extends beyond row 65536 (e.g. A65527:B65537) I get an error message
If I remove enough rows to drop the range below row 65536, the code works.
The code also works if I force the workbook to be read-only (and ensure that no-one else has a non-read-only version open).
Is this something I am doing wrong, or is this a bug in Excel 2013?
(Problem exists in both 32-bit and 64-bit versions. Also exists in Excel 2016.)
Well, in case this is still an open issue, I had the same problem and solved it by not specifying any row numbers in the range being queried. Example:
I was trying to query [SheetName$A1:W100000], which gave me exactly the same error you had. I then specified the range as [SheetName$A:W] and it weeerks!
Hope this helps!
I haven't been able to find an actual answer to my problem, so the best work-around I could come up with is to create an extra workbook, copy my range to a sheet in that workbook (starting at cell A1), save that workbook, and then use that workbook/worksheet as the source of the query.
(I originally thought I could get away with just creating a temporary worksheet in the existing workbook, i.e. without creating a temporary workbook, but problems occur if the user has two instances of Excel active - the Connection.Open event re-opens the workbook in the first instance of Excel, even though we are running the macros in the second instance, and therefore the re-opened workbook doesn't have the dummy worksheet in it. And I don't want to save a copy of the existing workbook with a dummy sheet in it.)
I would still prefer a more robust solution to this problem, so would love someone else to come up with another answer.