Selective import from CSV

  • Thread starter Thread starter CK
  • Start date Start date
C

CK

Hi!

I am writing a macro to import certain *.csv files to Excel. Each line in
the csv files represents a record. I wish Excel to import the records only
if it founds certain keywords.

Can anyone suggests me which sets of commands I can use to do the things
above?

Million thanks!!!
 
This works well by me :

Sub readFromCSV()

Dim oCnn As Object
Dim oCat As Object
Dim oRs As Object
Dim strPath As String, strFileName As String, strSQL As String

Set oCnn = CreateObject("ADODB.Connection")
Set oRs = CreateObject("ADODB.Recordset")

strPath = "D:\" 'this is the path to file
strFileName = "a.csv" 'name of the file
strSQL = "SELECT * FROM " & strFileName & " WHERE Field1 = 'x'"

oCnn.Open _
"Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=" & strPath
";Extensions=asc,csv,tab,txt"

oRs.Open strSQL, oCnn

With oRs
While Not .EOF

'here code for insertion to worksheet
'...
oRs.movenext
Wend
End With
oRs.Close
oCnn.Close

End Su
 
The reason I don't use Access is because I need to do some mathematical
analysis with the data.

I also need to filter out some of the unwanted records . . . if I do it
manually it will take me at least 1 hr per each of the hundreds CSV files.
 
Back
Top