-
Feb 16th, 2012, 04:06 AM
#1
Thread Starter
New Member
[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
-
Feb 16th, 2012, 04:35 AM
#2
Re: Subscript out of range, csv to listview
Originally Posted by cl1nch
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:
Private Sub Form_Load() Dim strParts() As String Dim strRow As String Dim xy As ListItem Dim n As Integer n = FreeFile ' it is recommended to use FreeFile to get file number Open "C:\purchased.csv" For Input As #n Do While Not EOF(1) Line Input #n, strRow If Trim$(strRow) <> vbNullString Then ' it is not empty, handle it strParts = Split(strRow, ",") If UBound(strParts) = 9 Then ' the row contains exact parts, handle it 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) ' Else ' uncomment these two lines if you want a warning about row with extra or less parts ' MsgBox "The row '" & strRow & "' has extra or less parts", vbExclamation End If End If Loop Close #n ' always close the file after finish reading it End Sub
Last edited by 4x2y; Feb 16th, 2012 at 04:41 AM.
-
Feb 16th, 2012, 08:45 PM
#3
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|