Consulting

Results 1 to 10 of 10

Thread: Solved: Text file extract

  1. #1
    VBAX Tutor
    Joined
    Feb 2006
    Posts
    295
    Location

    Solved: Text file extract

    hi,

    i have a 12000 page 35mb text file that i cannot import into excel. there are 2 fields that begin in the 14th character postion that i need to import into excel.
    these 2 fields will most likely appear twice on a page.

    for each row that begins with the word NTWK,
    the field begins in the 14th character that is 6 characters in length.

    for each row that begins with the word OWNER,
    the field begins in the 14th character that is 4 characters in length.

    these 2 fields will always be separated by 3 rows.


    any thoughts? is it possible?



    thanks
    zach

  2. #2
    can u please put some sample data?

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Is the first line in the text file a header line?

  4. #4
    VBAX Tutor
    Joined
    Feb 2006
    Posts
    295
    Location
    hi,

    i was incorrect about the column position
    the word NTWK and OWNER begins in column 2. and the fields that i need to extract are in column 16.
    the first line in the text file is not a header line
    attached is a santized copy of my text file
    thanks for your thoughts
    zach

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    A sample of the file in txt format would be better.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I can't test with your file but this assumes Tab delimiters as in the attached sample.

    [VBA]Sub GetText()
    Dim InputData, sTxt, i As Long
    Open "C:\test.txt" For Input As #1
    Do While Not EOF(1)
    Line Input #1, InputData
    sTxt = Split(InputData, vbTab)
    If sTxt(1) = "NTWK" Or sTxt(1) = "OWNER" Then
    i = i + 1
    Cells(i, 1) = sTxt(15)
    End If
    Loop
    Close #1 ' Close file.
    End Sub[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Tutor
    Joined
    Feb 2006
    Posts
    295
    Location
    hi malcolm,
    i couldn't get your code to work correctly with my data.
    here's the txt format file.
    i had originally tried to upload the txt file but was unable. i didn't think to zip the txt file...
    can you please look at this file


    thanks
    zach

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub GetText()
    Dim InputData, sTxt, i As Long
    Open "C:\sample.txt" For Input As #1
    Do While Not EOF(1)
    Line Input #1, InputData
    If Mid(InputData, 2, 4) = "NTWK" Then
    i = i + 1
    Cells(i, 1) = Mid(InputData, 14, 4)
    ElseIf Mid(InputData, 2, 5) = "OWNER" Then
    i = i + 1
    Cells(i, 1) = Mid(InputData, 14, 6)
    End If
    Loop
    Close #1 ' Close file.
    End Sub
    [/vba]

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Zach,
    Slight misundestasnding of "columns"
    XLD's code works (of course!), but it appears from your posted sample that the data to be returned starts in columns 16 & 17 respectively, but I'm sure you can sort that out.
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  10. #10
    VBAX Tutor
    Joined
    Feb 2006
    Posts
    295
    Location
    XLD & MD

    thanks for your guidance. this works! amazing stuff.

    zach

Posting Permissions

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