+ Reply to Thread
Results 1 to 9 of 9

Extracting data from a text file

  1. #1
    Registered User
    Join Date
    07-16-2012
    Location
    Macomb, MI
    MS-Off Ver
    Exel 2010
    Posts
    22

    Extracting data from a text file

    Hi All,
    I am new to VBA programming although I used to program in VBScript on web applications. Anyway with some help from other forums I have some code that extracts data from a flat text file. This file has a header line and a footer line and there are 2 lines of data per employee. The first line ends in 5 and the second line ends in 4. I will post the code I have so far and hope someone can help me finish it.....


    Please Login or Register  to view this content.
    OK, what I need to do is filter out specific trades for example I would like to show every trade except "Machine Repair" I will include a couple records from the flat file.....


    HDR20120710
    00101000036617 5270012008085197804171984102919730621DOE BJ52702B 2571370000000001600 5
    00101000036617JOHN 109080 55512345671952093000000001MACHINE REPAIR 4
    00101000099788 5270002010384198910301989103019891030SMITH DS52501C 257077S000000000000 5
    00101000099788BOB 109109 55523456781952082700000001ELECTICIAN-PROJECT COORD 4
    00101000099920 5270002011522199103181991031819901001BLUE SJ54302B 2570770001600000000 5
    00101000099920STEVEN 109109 55534567891960050400000001ELECTRICIAN 4
    00101000111236 5350601002558 1984102919740815ROAST DJ53502A 5904110000000000000 5
    00101000111236CHUCK 109109 55545678901952102800000001ENGINE ASSEMBLY ASSOC


    OK, above are 4 records.(don't forget it begins with a header line) so my code disregards that, but I try filter out "Machine Repair" and it doesn't work. I don't know what I'm doing wrong.

    Thanks in advance,
    Lenny
    Last edited by JBeaucaire; 07-16-2012 at 09:59 PM. Reason: Added code tags, as per forum rules. Don't forget!

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,631

    Re: Extracting data from a text file

    how about:
    Please Login or Register  to view this content.
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    07-16-2012
    Location
    Macomb, MI
    MS-Off Ver
    Exel 2010
    Posts
    22

    Re: Extracting data from a text file

    Hi Ben,
    I ran your code and it skips the header line and moves to the first line of data. It gets to the second line(Which the first 2 lines are the data for the complete first record. i.e. For every employee there are 2 lines of the flat file which contains all the information about that employee. Anyway it gets to the second line of the data and it puts everything into each cell except "Machine Repair". Meaning it is still putting in the last name, first name, senority date, and department of the "Machine Repair". What I need it to do is if it is in fact a "Machine Repair" trade then to skip that whole record and move on to the next one. I hope I am explaining this right.

    Thanks,

    Lenny
    Last edited by Cutter; 07-17-2012 at 10:59 AM. Reason: Removed whole post quote (Rule 12)

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,631

    Re: Extracting data from a text file

    Try this one:
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    07-16-2012
    Location
    Macomb, MI
    MS-Off Ver
    Exel 2010
    Posts
    22

    Re: Extracting data from a text file

    Ben that code worked great! Now the next question I have is: We excluded the "Machine Repair" trade, how would we change the code to ONLY include the "Machine Repair" trade and exclude everyone else?


    Thanks,
    Lenny

  6. #6
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,631

    Re: Extracting data from a text file

    The InStr function returns zero if the sought string is not found and greater than zero if found. In the macro, we checked for a zero, indicating that it was not found and proceeded to process the record.

    If the line were: If InStr(strLine2, "MACHINE REPAIR") > 0 , the lines following would only be executed if "Machine Repair" were found; all others would be ignored.
    Last edited by protonLeah; 07-18-2012 at 03:47 PM.

  7. #7
    Registered User
    Join Date
    07-16-2012
    Location
    Macomb, MI
    MS-Off Ver
    Exel 2010
    Posts
    22

    Re: Extracting data from a text file

    OK, So now I'm getting a little brave and am trying to sort and put each trade into a separate sheet, so far I only coded "Machine Repair" and "Electrician", anyway the following code adds a sheet called Machine Repair and closes and stops, if I step into the code it goes through the flat file gets all the "Machine Repair" and then when I comment out the line "On Error GoTo CloseIt:" I get an input past end of file error.... here is the code


    Please Login or Register  to view this content.


    Thanks,

    Lenny
    Last edited by Cutter; 07-18-2012 at 08:45 PM. Reason: Added code tags - again

  8. #8
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Extracting data from a text file

    @lamore48 Welcome to the forum

    Please notice that code tags have been added to your posts. The forum rules (Rule #3) require them so please keep that in mind and add them yourself whenever showing code in any of your future posts.
    Thanks.

  9. #9
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,631

    Re: Extracting data from a text file

    This macro will filter the trades to their own sheets:

    Please Login or Register  to view this content.

+ 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