View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
OssieMac OssieMac is offline
external usenet poster
 
Posts: 2,510
Default Fetch data from multiple .csv file paste into single sheet

Hi Deen,

Had problems posting this yesterday and it does not appear to have posted.
Will try again.

Call the code similarly to the previous example passing the initial path to
the sub.

Sub OpenMultiTxtFile(strPath As String)
Dim myTitle As String 'Dialog box title
Dim sFile As String 'Selected file name with path
Dim wbTxt As Workbook 'The text (csv) workbook
Dim arrSelected() 'Holds selected files
Dim i As Long

myTitle = "Select the required text files"

With Application.FileDialog(msoFileDialogFilePicker)
.Title = myTitle
.Filters.Clear
.AllowMultiSelect = True
.InitialFileName = strPath
.Filters.Add "Text files", "*.csv", 1
If .Show = False Then
MsgBox "User cancelled at file Open Dialog box"
Exit Sub 'If user cancels in dialog box
End If

'Assign selected file names to an array
ReDim arrSelected(1 To .SelectedItems.Count)
For i = 1 To .SelectedItems.Count
arrSelected(i) = .SelectedItems(i)
Next i
End With

For i = 1 To UBound(arrSelected)
Workbooks.OpenText Filename:=arrSelected(i)

Set wbTxt = ActiveWorkbook

wbTxt.Sheets(1).UsedRange.Copy _
Destination:=ThisWorkbook.Sheets("Inventory") _
.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)

wbTxt.Close
Next i

End Sub


--
Regards,

OssieMac