+ Reply to Thread
Results 1 to 8 of 8

Reading CSV text files

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-20-2007
    MS-Off Ver
    2003 & 2007
    Posts
    299

    Reading CSV text files

    I have this code that will read data from a csv file and put it in my excel sheet. However, could the code do 2 more things (listed in priority)?? 1. Start reading the file at the 3rd line, and only read every other line after that and 2. parse the data into separate columns based on the comma delimeter. I read somewhere that you could read the data into an array then loop the array into the columns to delimit it, but i don't know how to do that. Here is the code....thank you!!!!
    Sub testreader()
    Dim FileNo As Integer
    Dim CurrentLine As String
    Dim File As String
    Dim Msg As String
     
    Filename = "C:\Users\Harskey\Desktop\Book1.csv"
    FileNo = FreeFile
     
    Dim lRow As Long
    lRow = 2
    
    Open Filename For Input As FileNo
    Do While Not EOF(FileNo)
      Line Input #FileNo, CurrentLine
      If CurrentLine <> "" Then
        Cells(lRow, 1).Value = CurrentLine
      End If
      lRow = lRow + 1
    Loop
     
    Close #FileNo
    End Sub

  2. #2
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Reading CSV text files

    I would do it this way
    Sub testreader()
    Dim FileNo As Integer
    Dim CurrentLine As String
    Dim File As String
    Dim Msg As String
    Dim CurrentList As New Collection
    Filename = ThisWorkbook.Path & "\Book1.csv"
    FileNo = FreeFile
     Dim lRow As Long
    Open Filename For Input As FileNo
    Do While Not EOF(1)
    Debug.Print Loc(1)
      Line Input #1, CurrentLine
      If CurrentLine <> "" Then
        CurrentList.Add CurrentLine
      End If
    Loop
    For e = 3 To CurrentList.Count Step 2
     lRow = lRow + 1
    Cells(lRow, 1).Value = CurrentList.Item(e)
    Next
    Close #FileNo
    End Sub
    Last edited by pike; 02-21-2010 at 06:58 AM.
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  3. #3
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187

    Re: Reading CSV text files

    I think this approach is easier for a novice to understand.
    Note the texttocolumns function can be used manually from the spreadsheet

    The textToColumns can be preset to state what sort of data is in which columns. I have left this on automatic. If it causes a problem you will need to post some data and I (or somone) will help you with that.

    Sub testreader()
    Dim FileNo As Integer
    Dim CurrentLine As String
    Dim File As String
    Dim Msg As String
    Dim i As Integer
    Dim bUseLine As Boolean
    Dim rng As Range
    Dim Filename As String
     
    Filename = "C:\Users\Harskey\Desktop\Book1.csv"
    
    FileNo = FreeFile
     
    Dim lRow As Long
    lRow = 2
    Set rng = Cells(lRow, 1)
    
    Open Filename For Input As FileNo
    For i = 1 To 3  'ignore lines 1-3
        Line Input #FileNo, CurrentLine
    Next i
    
    bUseLine = True
    Do While Not EOF(FileNo)
      Line Input #FileNo, CurrentLine
        If bUseLine Then        'ie if bUseLine=true
            If CurrentLine <> "" Then
              Cells(lRow, 1).Value = CurrentLine
            End If
            lRow = lRow + 1
        End If
        bUseLine = Not (bUseLine)   'toggles TRUE to FALSE and FALSE to TRUE
      
    Loop
     
    Close #FileNo
    
    Set rng = rng.Resize(lRow - 1) 'minus one less than the start value
        rng.TextToColumns Destination:=rng, DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, Comma:=True
    End Sub


    click on the * Add Reputation if this was useful or entertaining.

  4. #4
    Forum Contributor
    Join Date
    02-20-2007
    MS-Off Ver
    2003 & 2007
    Posts
    299

    Re: Reading CSV text files

    Thankyou...Pikes example worked well, I incorporated the comma delimit aspect of tony h's example. Follow up question, How can you read just the last line of a file?

  5. #5
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187

    Re: Reading CSV text files

    It depends on what and why you are trying to do it.

    The simplest way is to use the same code as below to read through the file and just keep the last value.
    The fastest way is more complicated. You open the file in block mode and read and interpret the last blocks. It helps to know what the maximum line length is. Because of the variations this is more complex to programme.

    Note: if you are just trying to keep the last line of a file you are reading anyway the code below can be altered to make this available.

  6. #6
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Reading CSV text files

    Yep thats why I use the collection you can use the data again
    Just go to the last value in the collection

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Reading CSV text files

    That might be a lot of data to store, Pike, unless you have good reason to store it.
    Entia non sunt multiplicanda sine necessitate

  8. #8
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Reading CSV text files

    True, I usually extract all the good stuff , do the calx and just store the results

+ 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