View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
L.Mathe L.Mathe is offline
external usenet poster
 
Posts: 24
Default Extract data from csv file

My apologies for delay in replying, I had the flu and couldn't think
straight.

I looked more carefully at the type of files I need to seach for a
particular string, and found they are 'Excel Comma Separated Values'. The
files to be searched average 35,000 lines, and have, 1 believe, 120 columns
of data.

What I am attempting to do is search the 77th column for matching data, and
if there is a match, extract the data in the 47th column (19 digit number, so
need to extract as text), and also the data in the 110th column. When
opening the file using Note Pad, all the data is enclosed in " " and
separated by commas.

The workbook I want to extract the data to will always be basically blank.
I am hoping to have a user put a 'value' in Cell A1 then use a click button
to run the macro. It really doesn't matter what columns data goes to as long
as the data extracted is from the same line from the text file. IE Results
in WB:
Column A Column B
6888551119921316789 01/31/2010 15:10
6888551118195432688 02/13/2010 12:45

The code I found was as follows:
1-Their question: To extract data (the first three letters after the 2nd
comma, and the first 35 characters after the 7th comma) from a csv file (over
100,000 rows),
only after the 8th column matches a values in column A of my spreadsheet. The
two extracted data elements need to be stored in my worksheet in columns B
and C.

2- Reply: Sub Gettext()

Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

Dim Data(8)

'default folder
Folder = "C:\temp"
ChDir (Folder)

Set fsread = CreateObject("Scripting.FileSystemObject")
FName = Application.GetOpenFilename("CSV (*.csv),*.csv")

Set fread = fsread.GetFile(FName)
Set tsread = fread.OpenAsTextStream(ForReading, TristateUseDefault)

RowCount = 1
Do While tsread.atendofstream = False

InputLine = tsread.ReadLine

For i = 0 To 7
If InStr(InputLine, ",") 0 Then
Data(i) = Left(InputLine, InStr(InputLine, ",") - 1)
InputLine = Mid(InputLine, InStr(InputLine, ",") + 1)
Else
If Len(InputLine) 0 Then
Data(i) = InputLine
InputLine = ""
Else
Exit For
End If
End If
Next i
'check if 8th item is in column A
Set c = Columns("A:A").Find(what:=Data(7), LookIn:=xlValues, _
lookat:=xlWhole)
If Not c Is Nothing Then
c.Offset(0, 1) = Left(Data(2), 3)
c.Offset(0, 2) = Left(Data(7), 35)
End If
Loop
tsread.Close
End Sub

Unfortunatley I have not been able to modify this (I can hardly read it)!

Thanks


--
Linda


"KC" wrote:

Interesting exercise.
I am guessing that you have one workbook with one worksheet where
A1, B1 only are filled. Nothing further;
Only search in each csv file for 76th and 109th comma,
In what way is the matching done please? as the following 19 positions are
DIGITS only.

"L.Mathe" wrote in message
...
I was looking through this discussion group, found something close to what
I
need, but not being a programmer, I haven't been able to modify it to what
I
am attempting to do and I hope someone can help.

The .csv files are split into groups by month (ie: "c:\Jan\file
name.csv)".
I need to search within the group of csv files and extract data into an
Excel
file. What I would like to do is if Cell A1 in my active wb matches the
data
to the right of the 76 comma in the csv file, extract the 'text' value
(must
be specified as text as this data is a 19 digit number and can't have it
tuncated), in cell A2. Then in cell B2, extract the data that is to the
right of the 109 comma. Continue searching the current file and loop
through
all remaining files, extract subsequent data into the next line below.

Hopefully this is possible and someone can help!

Thanks
--
Linda



.