I'm trying to use sql on data in a named range in excel 2010. I was able piece meal the following code, but it errors with the following message: "External table is not in the expected format." The named range is "New". Is this fixable?
Please note that once i get a working code (if that's possible) my sql statement will contain where clauses and such, this is just doing something simple for testing.
Code:
Public Sub QueryNamedRange()
Dim Recordset As ADODB.Recordset
Dim ConnectionString As String
ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & ThisWorkbook.FullName & ";" & _
"Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
Dim SQL As String
SQL = "SELECT * FROM New;"
Set Recordset = New ADODB.Recordset
On Error GoTo Cleanup
Call Recordset.Open(SQL, ConnectionString, _
CursorTypeEnum.adOpenForwardOnly, LockTypeEnum.adLockReadOnly, _
CommandTypeEnum.adCmdText)
Call Range("L5").CopyFromRecordset(Recordset)
Cleanup:
Debug.Print Err.Description
If (Recordset.State = ObjectStateEnum.adStateOpen) Then
Recordset.Close
End If
Set Recordset = Nothing
End Sub
Please note that once i get a working code (if that's possible) my sql statement will contain where clauses and such, this is just doing something simple for testing.