Consulting

Results 1 to 7 of 7

Thread: Losing last line when readig ASCII file into Array

  1. #1

    Losing last line when readig ASCII file into Array

    Why do I lose the last line of text, with this code? If I check the variable InputString, before I write my array to the worksheet it contains the last line, yet, my array doesn't contain it?

    [VBA]
    Public Sub TextReader()
    Dim FileName As String, Path As String
    Dim InputString As String, FileNum As Integer
    CVSArray() As String

    Path = "M:\TEST\"
    FileName = Dir(Path & "*.txt")

    Do Until FileName = vbNullString
    FileNum = FreeFile ' next file number
    ReDim CVSArray(0) 'clear the array
    i = 0 'line counter

    Open Path & FileName For Input Access Read Shared As #FileNum

    Do While Not EOF(FileNum)

    ReDim Preserve CVSArray(0 To i)
    Line Input #FileNum, InputString

    CVSArray(i) = InputString

    i = i + 1 'increment line counter
    Loop ' until the last line is read

    NextRow = Sheets(1).Range("A65536").End(xlUp).Offset(1, 0).Row
    Sheets(1).Range(Cells(NextRow, 1), Cells(NextRow, i - 1)) = CVSArray

    Close #FileNum
    FileName = Dir()
    Loop

    End Sub

    [/VBA]

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Works fine for me. Can you post the offending text file?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Even something as simple as this:
    1
    2
    3
    4

    Cells A:C are populated, but Cell D which should contain the number 4 is blank

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I see what it is, it is in the code to drop it into the worksheet ( I only tested upto loading the array previously)

    [vba]

    Sheets(1).Range(Cells(NextRow, 1), Cells(NextRow, i - 1)) = CVSArray
    [/vba]

    should be

    [vba]

    Sheets(1).Range(Cells(NextRow, 1), Cells(NextRow, i)) = CVSArray
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Thanks, lord only kows why that was there, remnants of a previous rendition of this code I presume! thanks

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    My thought was that you did it because you knew that the array was zero based (but of course cells are not), a simple error (done the same sort of thing many times myself).
    Last edited by Bob Phillips; 10-22-2007 at 11:57 AM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Yeah in hindsight that was the reason. I've been thru so many iterations of this code trying to get it to properly parse the data, that I started to lose track.

Posting Permissions

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