Results 1 to 8 of 8
  1. #1
    2 Star Lounger
    Join Date
    Sep 2003
    Location
    Toronto, Ontario, Canada
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Reading files (VBA 2003)

    I have opened a text file for input and I would like to read some information located at the bottom the text file. Most of this text file contains jibberish (ascii), however near the bottom is the following (this is an example):

    ##$$TABFILE$$##Ryan, President, 124
    Jordan, Vice-president, 125
    Henry, COO, 126

    Is there anyway I can search through all this jibberish in the text file until VB finds the string "##$$TABFILE$$##" so that I can begin to grab the values?

  2. Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Reading files (VBA 2003)

    Like this?

    <pre>Public Sub SkipJibberish()
    Dim iFileNo As Integer
    Dim strInp As String
    iFileNo = FreeFile
    Open "C:WorkJIBB.txt" For Input As #iFileNo
    Do While Not EOF(iFileNo)
    Line Input #iFileNo, strInp
    If Left(strInp, 15) = "##$$TABFILE$$##" Then Exit Do
    Loop
    'Process the rest of the file here
    Close #iFileNo
    End Sub
    </pre>

    Legare Coleman

  4. #3
    2 Star Lounger
    Join Date
    Sep 2003
    Location
    Toronto, Ontario, Canada
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Reading files (VBA 2003)

    That would be good however the TabFile string does not occur at the beginning of the line. There is jibbreish before it. Would I be able to do something along the lines of:
    If Contains(strInp) = "##$$TABFILE$$##" then exit Do

  5. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 16 Times in 16 Posts

    Re: Reading files (VBA 2003)

    Try

    If InStr(strInp, "##$$TABFILE$$##") > 0 Then Exit Do

  6. #5
    2 Star Lounger
    Join Date
    Sep 2003
    Location
    Toronto, Ontario, Canada
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Reading files (VBA 2003)

    Thanks for the input. That solution worked for me in practice, however when I put it to the test, the function didnt work. The reason it didnt work is because the text file which contains the 'jibberish' does not have any character returns. So it doesn't matter whether I use:
    Line Input #ifile
    or
    Input #ifile
    because either way, the string variable hits its limit before the phrase "##$$TABFIELD$$##".
    So instead, I figured I would read the file character by character by using:
    Open "c:blah.txt" for Binary as ....
    and I check to see if the last two characters are $, # and the current is #. That works fine, and I have a counter, so I know where in the file the useable data begins.

    My question is: how can I start reading the useable data, knowing that the jibberish ends x number of characters into the text file?
    Can I open a file for Input and start at position x?

  7. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 16 Times in 16 Posts

    Re: Reading files (VBA 2003)

    You can use the Seek instruction to position the pointer into the file where reading will continue.

    Dim f As Integer
    f = FreeFile
    Open "..." For Binary As #f
    Seek #f, 235
    ...

  8. #7
    2 Star Lounger
    Join Date
    Sep 2003
    Location
    Toronto, Ontario, Canada
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Reading files (VBA 2003)

    Does 'Seek' only work when files are opened for Binary, or does it work for Input and Random?

  9. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 16 Times in 16 Posts

    Re: Reading files (VBA 2003)

    It works for files opened for Input or Random too. If you're reading a file line by line, though, Seek will not necessarily position you at the beginning of a line, so you may end up reading part of a line.

Posting Permissions

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