SQL on Named Range - VBA

erock24

Well-known Member
Joined
Oct 26, 2006
Messages
1,163
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?

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.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Forum statistics

Threads
1,222,618
Messages
6,167,081
Members
452,094
Latest member
Roberto Saveru

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top