+ Reply to Thread
Results 1 to 15 of 15

Import Text File VBA Script

  1. #1
    Registered User
    Join Date
    03-22-2011
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    48

    Import Text File VBA Script

    Hi,

    I'm really bad at writing VBA scripts so I was hoping that someone could help me. I have a text file with data that I would like to import in to Excel. I want it to import only certain parts of the data and there's no good way of doing it using the Excel import from text feature. I think it's probably easier to show you what I'm looking for with an example of the data and an example of how I want it to be imported. I've attached a text file with two entries of the data and an Excel file with how I would like the data imported. Each entry in the text file is separate by a blank line. I hope this makes sense. I appreciate any help.

    Thanks.
    Attached Files Attached Files

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,995

    Re: Import Text File VBA Script

    If I were you, I would take the simplistic approach ... import the raw data as it stands and then see what you need to do to manipulate it. The first part should be very straightforward ... just record a macro as you open or import the file.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    03-22-2011
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    48

    Re: Import Text File VBA Script

    When I import the raw data in to Excel, it puts each line in a row in to a single column. I'm not sure how I could then manipulate the data to only extract the wanted data. This text file contains a few hundred entries like in the example files so I need a good way to automate the process.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,995

    Re: Import Text File VBA Script

    If you want to read the text file, you would have to read the line, test it and do something with it. If you open it in Excel it will all come in in one go. You could use fixed width columns to split the data or use a delimiter ... space, maybe ... to split it up. Then you can decide what to do with it. You could use AutoFilter to select specific types of data ...

    Lots you can do in Excel but you'd need a bespoke VBA application to read and process the text file.

    Regards, TMS

  5. #5
    Registered User
    Join Date
    03-22-2011
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    48

    Re: Import Text File VBA Script

    Thanks. I think I may have found a way to get the data I want. Not completely automated, but better than going through that text file manually. Thanks for the help.

  6. #6
    Registered User
    Join Date
    03-22-2011
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    48

    Re: Import Text File VBA Script

    Well never mind my idea. I thought there was an equal number of rows between the "true" row and the related value rows. Now I'm stuck again without any ideas. I don't know how to create a function within Excel to do what I'm wanting. Any extra help would be appreciated. I've attached the entire spreadsheet how I have it now so you can see what it looks like imported and the difference in rows for each entry.
    Attached Files Attached Files

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,995

    Re: Import Text File VBA Script

    Just so that you know I haven't forgotten about you ...

    The following code assumes that the raw data has been imported into the Data sheet.

    It will create a Criteria sheet and an Output sheet, if they do not exist, populate the Criteria sheet with Advanced filter selection criteria, filter the raw data to the Output sheet and then removes duplicate repId entries.

    The final stage would be to process the filtered data in the Output sheet and format it in the results sheet. And maybe delete the Criteria and Output sheets. Maybe tomorrow

    As it stands, the code has reduced the raw data from 265160 rows to 1280 rows; less than 5%.

    The only quirk is that there are some repId entries where "successful" was False. These can be ignored/eliminated in the final stage of processing.

    Please Login or Register  to view this content.

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,995

    Re: Import Text File VBA Script

    Final code. Try this:

    Please Login or Register  to view this content.

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,995

    Re: Import Text File VBA Script

    A better version ... this ignores success=false records

    Please Login or Register  to view this content.

  10. #10
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Import Text File VBA Script

    hi aharb, please check attachment, press Run
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    03-22-2011
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    48

    Re: Import Text File VBA Script

    Wow. That's a lot of code that I have no clue how it works. I'm going to give it a try and see how it goes. Thank you so much for the help. I'll report back and let you know how it went.

  12. #12
    Registered User
    Join Date
    03-22-2011
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    48

    Re: Import Text File VBA Script

    I was able to get the macro to run and import all of the data exactly how I wanted. I appreciate the help. One day I hope to understand VBA better and might be able to do some of this on my own.

    Thanks again.

  13. #13
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,995

    Re: Import Text File VBA Script

    My code assumes that you have opened the text file and it is in a sheet named Data. It then creates a a Criteria sheet, an Output Sheet and a Results sheet. It uses the Criteria sheet to drive an Advanced filter, copying the filtered data to the Output sheet. It then processes the Output sheet to create the Results sheet in the format that you have indicated. I note that you have added some formulae to indicate the rows of the success tags. Finally, it tidies up after itself so you are left with the source data and the Results summary.

    Watersev's code processes the text file directly.

    It is interesting to note that the original text file (processed in Watersev's code) is some 300 lines. The data in the later Excel spreadsheet is around 26500+ lines.

    It all depends on what you need to achieve.

    I haven't done a time comparison between the two solutions but it might be worth doing that.

    Regards, TMS

  14. #14
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,995

    Re: Import Text File VBA Script

    Forget it. Done the time trial ... that is quite impressive!

  15. #15
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,995

    Re: Import Text File VBA Script

    aharb: the file I was using is attached.

    See if this works for you.

    Regards, TMS
    Attached Files Attached Files

+ 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