Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
 
 
 
 
 
 
 
Old Pages
 
Old Index
Site Map
What's New
 
Books
How To
Tips & Tricks
Tutorials
Stories
Performance
Essays
Links
Q & A
New in VB6
Free Stuff
Pictures
 
 
 
TitleTotal the values in a specific field in a comma-separated value (CSV) file
KeywordsCSV, comma-separated value, total, fields
CategoriesFiles and Directories
 
Read the file into a string. Use Split to break it into lines.

For each line, use Split to break the line into fields. Total the desired field.

 
Private Sub cmdGo_Click()
Dim file_num As Integer
Dim txt As String
Dim lines() As String
Dim i As Long
Dim fields() As String
Dim delimiter As String
Dim field_num As Integer
Dim total As Long

    delimiter = txtDelimiter.Text
    field_num = txtField.Text

    ' Read the file.
    file_num = FreeFile
    Open txtFilename.Text For Input As #file_num
    txt = Input(LOF(file_num), file_num)
    Close #file_num

    ' Process the lines.
    lstValues.Clear
    lines = Split(txt, vbCrLf)
    For i = LBound(lines) To UBound(lines)
        ' Break the line into fields.
        fields = Split(lines(i), delimiter)
        If UBound(fields) >= field_num Then
            total = total + CLng(fields(field_num))
        End If

        ' Display the value.
        lstValues.AddItem lines(i)
    Next i

    ' Display the total.
    MsgBox "Total: " & Format$(total)
End Sub
 
 
Copyright © 1997-2003 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated