View Single Post
  #6   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

Hi Joel,

This code is BRILLIANT! I just need one other piece to it, I need also to
extract the data in column 47 (note, the data is a 19 digit number so it must
be defined as 'Text'. I tried to modify what you sent me as follows, but it
didn't work:

Do
Data1 = CSVSht.Cells(c.Row, 110)
Data2 = CSVSht.Cells(c.Row, 47)

With ThisWorkbook.Sheets(DestSht)
..Range("A" & RowCount) = FName
..Range("B" & RowCount) = RowCount
..Range("C" & RowCount) = Data1
..Range("D" & RowCount) = Data2

RowCount = RowCount + 1
End With

I hope you can provide a little further assistance with this. The amount of
manual work this piece of code will save is invaluable.

Thank you
--
Linda


"joel" wrote:


this code will open a folderPicker to get the correct folder and then
search every CSV file in the folder using column 77 and getting data in
column 110.


Sub GetData()
DestSht = "sheet1"
With ThisWorkbook.Sheets(DestSht)
SearchData = .Range("A1").Text
.Columns("A:B").NumberFormat = "@"
End With

'Declare a variable as a FileDialog object.
Dim fd As FileDialog

'Create a FileDialog object as a Folder Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFolderPicker)

'Declare a variable to contain the path
'of each selected item. Even though the path is a String,
'the variable must be a Variant because For Each...Next
'routines only work with Variants and Objects.
Dim vrtSelectedItem As Variant

'Use a With...End With block to reference the FileDialog object.
With fd

'Use the Show method to display the File Picker dialog box and
return the user's action.
'The user pressed the action button.
If .Show = -1 Then

'Step through each string in the FileDialogSelectedItems
collection.
For Each Folder In .SelectedItems
Call ReadCSV(Folder, SearchData, DestSht)

Next Folder
End If
End With

'Set the object variable to Nothing.
Set fd = Nothing
End Sub

Sub ReadCSV(ByVal Folder As Variant, _
ByVal SearchData As String, _
ByVal DestSht)

Dim Data As String

LastRow = ThisWorkbook.Sheets(DestSht) _
.Range("A" & Rows.Count).End(xlUp).Row
NewRow = LastRow + 1
RowCount = NewRow
FName = Dir(Folder & "\*.csv")
Do While FName < ""

Workbooks.OpenText Filename:=Folder & "\" & FName, _
DataType:=xlDelimited, Comma:=True
Set CSVFile = ActiveWorkbook
Set CSVSht = CSVFile.Sheets(1)
'check if data exists in column 77
Set c = CSVSht.Columns(77).Find(what:=SearchData, _
LookIn:=xlValues, lookat:=xlWhole)
If Not c Is Nothing Then
FirstAddr = c.Address
Do
Data = CSVSht.Cells(c.Row, 110)
With ThisWorkbook.Sheets(DestSht)
.Range("A" & RowCount) = FName
.Range("B" & RowCount) = RowCount
.Range("C" & RowCount) = Data
RowCount = RowCount + 1
End With
Set c = CSVSht.Columns(77).FindNext(after:=c)
Loop While Not c Is Nothing And c.Address < FirstAddr
End If
CSVFile.Close savechanges:=False

FName = Dir()
Loop
End Sub


--
joel
------------------------------------------------------------------------
joel's Profile: 229
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=180054

Microsoft Office Help

.