Consulting

Results 1 to 7 of 7

Thread: Solved: Trying to use Line Input, but only vbLF

  1. #1
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location

    Solved: Trying to use Line Input, but only vbLF

    I need to read a very large tab delimited text file (mainframe generated), but each line is terminated with a LF, not a CR-LF pair, or a CR, which Line Input needs.

    Because of the file size and because a lot of data will not be stored because of the data in a line, I didn't want to just brute force load it and do text to columns.

    Right now, I'm thinking about

    1. reading and writing byte-wise a copy of the file, replacing LF with CR's
    2. using Line Input to pull in a single record
    3. Split() to seperate at the tabs
    4. Then storing the record if it meets certain critera

    Seems that that will take even more time

    Suggestions anyone?

    Paul

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    Meant to include the current incarnation of the "proof of concept" macro I'm using

    [VBA]
    Sub drv2()
    Dim iFile1 As Long, iFile2 As Long
    Dim sBuffer As String

    Dim i As Long

    iFile1 = FreeFile
    Open "File.txt" For Binary As iFile1

    iFile2 = FreeFile
    Open "File.out" For Output As iFile2
    i = 1
    While Not EOF(iFile1)
    sBuffer = Input(65536, #iFile1)
    sBuffer = Replace(sBuffer, vbLf, vbCr & vbLf)
    Print #iFile2, sBuffer
    i = i + 1
    Application.StatusBar = i
    Wend

    Close #iFile2
    Close #iFile1
    End Sub
    [/VBA]

    It works, but somehow seems unnecessary to have to do it this way.

    For example, if the file records are already terminated with CR-LK, it'll fail

    Paul

  3. #3
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Seems like you might have a vbCR twice sometimes after the first Replace. One could then replace the two codes for one.

    Seems like I dealt with an issue like yours once.

    Does vbCR & vbLF differ from vbCRLF?

    Here is an interesting macro for importing a big csv. http://www.mrexcel.com/forum/showthread.php?t=505997

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Seems like I did something like what they did in this thread to handle the vbCRLF. http://www.mrexcel.com/forum/showthread.php?t=505975

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    Ken -

    thanks for the links

    a. Seems like you might have a vbCR twice sometimes after the first Replace.
    b. Does vbCR & vbLF differ from vbCRLF?
    a. I'll trace through the output to see
    b. No, I just getting old and forgetful

    aul

  6. #6
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    Quote Originally Posted by Paul_Hossler
    Ken -

    thanks for the links



    a. I'll trace through the output to see
    b. No, I just getting old and forgetful

    aul
    Yes, you forgot "P" in your name!
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    See -- that proves it!!

    Can I blame my keyboard?

    Paul

Posting Permissions

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