Query data from closed workbook

Excalibur13

New Member
Joined
Jun 14, 2014
Messages
25
Not sure why I'm getting an error (Data type mismatch). I have a very large excel file with over 100,000 rows (very, very large and takes like 2 min to open). Im trying to query information without opening the workbook. This is what I have below and I'm getting an error at

Code:
rs.Open query, cnStr, adOpenUnspecified, adLockUnspecified

In the Data.csv there are alot of headers all in row 1, one of them is M_NB and this is what I want to query (more specifcally the rows where M_NB = 4506118). Also the only sheet in the workbook is called Sheet1

Code:
Sub Pull_Data_from_Excel_with_ADODB()


    Dim cnStr As String
    Dim rs As ADODB.Recordset
    Dim query As String


    Dim fileName As String
    fileName = "[COLOR=#000000][FONT=Helvetica Neue]c:\users\Jim\desktop[/FONT][/COLOR]\New folder\Data.csv"


    cnStr = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
               "Data Source=" & fileName & ";" & _
               "Extended Properties=Excel 12.0"


    query = "SELECT * FROM Sheet1$] WHERE [M_NB] = '4506118'"


    Set rs = New ADODB.Recordset
[U][B]    rs.Open query, cnStr, adOpenUnspecified, adLockUnspecified[/B][/U]


    Cells.Clear
    
    Dim cell As Range, i As Long
    
    Range("A2").CopyFromRecordset rs
    With Range("A1").CurrentRegion
        For i = 0 To rs.Fields.Count - 1
            .Cells(1, i + 1).Value = rs.Fields(i).Name
        Next i
        .EntireColumn.AutoFit
    End With




End Sub

Thanks for everyone's help who can assist
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Forum statistics

Threads
1,215,022
Messages
6,122,721
Members
449,093
Latest member
Mnur

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