+ Reply to Thread
Results 1 to 9 of 9

Import specific row from multiple .csv files

  1. #1
    Registered User
    Join Date
    12-08-2008
    Location
    Boston, MA
    Posts
    6

    Import specific row from multiple .csv files

    Hi

    I've tried googling various solutions for this, but have come up short.

    I have thousands of the file attached in .csv format (to allow attaching, I put it into .txt format). What I'd like to do is pin point a row. For the sake of this example, 132, then copy and paste that into a master excel sheet.

    I'd then want it to open the next .csv file, copy the same row and insert it onto a new row on the same master excel sheet. And continue to do this until all the .csv files are completed.

    That row would preferably be a variable, though I can just adjust this in the code itself.

    Has anyone got a VBA solution for this? I have at least gotten coding to work to convert the .csv files to .xls files - not sure if that helps, but if it does, I can do that.

    Thank you in advance for any tips!
    Attached Files Attached Files
    Last edited by hobbes79; 09-26-2011 at 12:13 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Import specific row from multiple .csv files

    Hello hobbes79,

    You cite row 132 as an example which suggests you will be looking for different rows in actual practice. What will be the deciding factors for selecting a row in the text file? Are all the text files in the same folder?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Valued Forum Contributor
    Join Date
    06-19-2010
    Location
    Holywell, N Wales, UK
    MS-Off Ver
    Excel 2013
    Posts
    470

    Re: Import specific row from multiple .csv files

    Hi hobbes79
    Please try the attached
    With acknowlegement to Leith's Qs I have hardcoded the various parameters into my code at present - you will need to set these to suit your context/setup.
    Also, this is my first attempt at using Microsoft Scripting Runtime so you will need to set a reference to this before you run the macro.
    Assumptions for testing:
    .. files with a .txt extension
    .. the text files are in a single folder (I used 4 copies of your file (renamed 1 - 4) for testing)
    .. outputted the file name before the data line (for testing)
    .. that there are 10 lines of 'header' data in each file before the data starts
    .. set the data line to extract to 10 (not 132) for testing - change 'recno'
    Run macro called 'extractxt'
    Attached Files Attached Files

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Import specific row from multiple .csv files

    assuming the csv-files in folder "E:\"

    Please Login or Register  to view this content.



  5. #5
    Registered User
    Join Date
    12-08-2008
    Location
    Boston, MA
    Posts
    6

    Re: Import specific row from multiple .csv files

    Thank you for your help to this point.

    Leith, yes, I would want to change from row 132. Though I'm comfortable with this being hard coded and I'd just change it to suit.

    barryleajo, thank you very much. But the assumption of .txt files is a problem. The files are in .csv format and stored on a server as read only. I converted the file to a .txt as a one off to allow me to attach it here (for some reason, the forum won't allow me to attach a .csv file). So it's a show stopper to have the .txt assumption. Sorry if I caused any confusion there.

    snb, I tried running yours, and it works Thank you! But I'm having a problem - it's bringing the data in as one line like in the original csv. I try importing the data to excel as delimited with the delimiter being the comma, but it doesn't seem to be working. It's as if it can't see the comma. Any ideas?

    Very close! Thank you for all your help to this point.

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Import specific row from multiple .csv files

    Of course you are familiar with the method 'Texttocolumns'.
    You can add that to the macro.

  7. #7
    Registered User
    Join Date
    12-08-2008
    Location
    Boston, MA
    Posts
    6

    Re: Import specific row from multiple .csv files

    I'm struggling a bit I'm afraid. I put in the new code below, but I'm getting Runtime Error 424 and getting an "Object Required" error. Any tips would be appreciated.

    Sub snb()
    For Each it In CreateObject("scripting.filesystemobject").getfolder("c:\ep600").Files
    Open it For Input As 1
    c01 = c01 & vbCr & Split(Input(LOF(1), 1), vbCrLf)(132)
    Close
    Next
    sn = Split(Mid(c01, 2), vbCr)

    Cells(1).Resize(UBound(sn) + 1) = Application.Transpose(sn)

    Set objRange = objExcel.Range("A1").EntireColumn
    Set objRange2 = objExcel.Range("B1")

    objRange.TextToColumns objRange2, , , , , , , , , , True


    End Sub

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Import specific row from multiple .csv files

    Please wrap your code in codetags.....


    adding 1 line suffices:
    If comma is the 'field' separator:

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    12-08-2008
    Location
    Boston, MA
    Posts
    6

    Re: Import specific row from multiple .csv files

    You're awesome. Works great. Thank you very much.

+ 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