+ Reply to Thread
Results 1 to 3 of 3

Import row from a folder with .csv files

  1. #1
    Registered User
    Join Date
    06-28-2011
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2010, Excel 2011 for mac
    Posts
    82

    Import row from a folder with .csv files

    Hi,


    I have a folder of 4000 .csv files containing data. For every file the data is in row 8.

    I would like to create a macro that imports row 8 from all the csv files in the folder and adds them to the current worksheet.

    anybody have some suggestions to how to go about this?

    regards,
    kbka

  2. #2
    Registered User
    Join Date
    06-28-2011
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2010, Excel 2011 for mac
    Posts
    82

    Re: Import row from a folder with .csv files

    So far, I've found part of the solution posted here:

    http://www.excelforum.com/excel-prog...te-sheets.html

    with the code

    [code]
    Sub Example12()
    Dim MyPath As String
    Dim FilesInPath As String
    Dim MyFiles() As String
    Dim SourceRcount As Long
    Dim Fnum As Long
    Dim mybook As Workbook
    Dim basebook As Workbook

    'Fill in the path\folder where the files are
    'on your machine
    MyPath = "c:\Data"

    'Add a slash at the end if the user forget it
    If Right(MyPath, 1) <> "\" Then
    MyPath = MyPath & "\"
    End If

    'If there are no Excel files in the folder exit the sub
    FilesInPath = Dir(MyPath & "*.csv")
    If FilesInPath = "" Then
    MsgBox "No files found"
    Exit Sub
    End If

    On Error GoTo CleanUp

    Application.ScreenUpdating = False
    Set basebook = ThisWorkbook

    'Fill the array(myFiles)with the list of Excel files in the folder
    Fnum = 0
    Do While FilesInPath <> ""
    Fnum = Fnum + 1
    ReDim Preserve MyFiles(1 To Fnum)
    MyFiles(Fnum) = FilesInPath
    FilesInPath = Dir()
    Loop

    'Loop through all files in the array(myFiles)
    If Fnum > 0 Then
    For Fnum = LBound(MyFiles) To UBound(MyFiles)
    Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
    mybook.Worksheets(1).Copy after:= _
    basebook.Sheets(basebook.Sheets.Count)

    On Error Resume Next
    ActiveSheet.Name = mybook.Name
    On Error GoTo 0

    ' You can use this if you want to copy only the values
    ' With ActiveSheet.UsedRange
    ' .Value = .Value
    ' End With

    mybook.Close savechanges:=False
    Next Fnum
    End If
    CleanUp:
    Application.ScreenUpdating = True
    End Sub

    [\code]




    I now need to replace some of the contents in the 'For' loop to select row 8, copy it, and paste it into the main work sheet defined as 'basebook' in the above code.

    Have I hard time getting it to work.

    any help is much appreciated.

    regards,
    kbka

  3. #3
    Registered User
    Join Date
    06-28-2011
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2010, Excel 2011 for mac
    Posts
    82

    Re: Import row from a folder with .csv files

    I'm thinking it would be fine for the code to paste it into row number equal to the number of 'Fnum'. So the first run is pasted into row 1, second run row 2 etc.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1