Results 1 to 3 of 3

Thread: [RESOLVED] Subscript out of range, csv to listview

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Location
    PHL
    Posts
    5

    Resolved [RESOLVED] Subscript out of range, csv to listview

    Hi, Im receiving the error Subscript out of range whenever I try to populate my listview using csv.

    Is there a limit for the data the listview can hold?

    If the csv has only about 1000 rows, the listview displays it without errors. But my csv file usually holds more than 3000 rows.


    The code goes like this,
    Code:
    Private Sub Form_Load()
    
        Dim strParts() As String
        Dim strRow As String
        Dim xy As ListItem
        
        Open "C:\purchased.csv" For Input As #1
        
        Do While Not EOF(1)
            Line Input #1, strRow
            strParts = Split(strRow, ",")
            Set xy = ListView1.ListItems.Add(, , strParts(0))
            xy.SubItems(1) = strParts(1)
            xy.SubItems(2) = strParts(2)
            xy.SubItems(3) = strParts(3)
            xy.SubItems(4) = strParts(4)
            xy.SubItems(5) = strParts(5)
            xy.SubItems(6) = strParts(6)
            xy.SubItems(7) = strParts(7)
            xy.SubItems(8) = strParts(8)
            xy.SubItems(9) = strParts(9)
    
        Loop
       
    End Sub

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Subscript out of range, csv to listview

    Quote Originally Posted by cl1nch View Post
    If the csv has only about 1000 rows, the listview displays it without errors. But my csv file usually holds more than 3000 rows.
    The file may contains empty line(s) after the first 1000 lines or a line contains less 10 parts, therefore you have to add some checks on the data you read from the file.

    Try this
    vb Code:
    1. Private Sub Form_Load()
    2.  
    3.     Dim strParts() As String
    4.     Dim strRow As String
    5.     Dim xy As ListItem
    6.     Dim n As Integer
    7.    
    8.     n = FreeFile ' it is recommended to use FreeFile to get file number
    9.     Open "C:\purchased.csv" For Input As #n
    10.    
    11.     Do While Not EOF(1)
    12.         Line Input #n, strRow
    13.        
    14.         If Trim$(strRow) <> vbNullString Then ' it is not empty, handle it
    15.             strParts = Split(strRow, ",")
    16.             If UBound(strParts) = 9 Then ' the row contains exact parts, handle it
    17.                 Set xy = ListView1.ListItems.Add(, , strParts(0))
    18.                 xy.SubItems(1) = strParts(1)
    19.                 xy.SubItems(2) = strParts(2)
    20.                 xy.SubItems(3) = strParts(3)
    21.                 xy.SubItems(4) = strParts(4)
    22.                 xy.SubItems(5) = strParts(5)
    23.                 xy.SubItems(6) = strParts(6)
    24.                 xy.SubItems(7) = strParts(7)
    25.                 xy.SubItems(8) = strParts(8)
    26.                 xy.SubItems(9) = strParts(9)
    27. '            Else ' uncomment these two lines if you want a warning about row with extra or less parts
    28. '                MsgBox "The row '" & strRow & "' has extra or less parts", vbExclamation
    29.             End If
    30.         End If
    31.     Loop
    32.    
    33.    Close #n ' always close the file after finish reading it
    34. End Sub
    Last edited by 4x2y; Feb 16th, 2012 at 04:41 AM.



  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Location
    PHL
    Posts
    5

    Re: Subscript out of range, csv to listview

    @4x2y
    Thanks for the code, it works!

    I also found out what causes the problem, and yes your right. While it should only be 10 columns, some of the lines contains less parts and some contains more. It's probably because of supposed to be single entries with "," in it. For example "Bond Paper, Short, Long".

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width