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
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
Thanks for everyone's help who can assist
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