Sign in or 

People just like you can add or edit the content on this site. If you want to try editing, but aren't ready to add to this site, try our demo area.
Read more about editing pages at Wetpaint Central.
)
' Reads in a Comma Seporated Values (CSV) fileEnd Function
' and puts it all into collections with nice labels.
' Parameters: vstrListFilePath - the path and file name of the CSV file.
' This function uses the Split function to carve up CSV file
' and load it all into collections.
' In the CSV file, the first row is for field names (headers)
' and only the right most column can contain commas.
' That's usually used for comments.
' --- EXAMPLE ---
' Set cData = GetCSVCollection(Command)
' For Each cFields In cData' Debug.Print cFields("Header")' Next
Dim arrRecs() As String ' <-- array of records or lines
Dim arrFieldNames() As String ' <-- field names are in the first line
Dim arrVals() As String ' <-- array of values in one line goes to colVals
Dim colRecs As New VBA.Collection ' <-- where all this data ends up
Dim colVals As VBA.Collection ' <-- values from one line with field names as keys
Dim lngValUBound As Long ' <-- max index of vals in rec
Dim lngFieldNamesUBound As Long
Dim i As Long ' <-- row loop index
Dim j As Long ' <-- column loop index
arrRecs = Split(GetFileContents(vstrListFilePath), vbCrLf)
arrFieldNames = Split(arrRecs(0), ",")
lngFieldNamesUBound = UBound(arrFieldNames)For i = 1 To UBound(arrRecs)Next
arrVals = Split(arrRecs(i), ",")
' --- This handles commas in the last column. ---
lngValUBound = UBound(arrVals)
If lngValUBound > lngFieldNamesUBound ThenFor j = lngFieldNamesUBound + 1 To lngValUBoundEnd IfarrVals(lngFieldNamesUBound) = arrVals(lngFieldNamesUBound) + "," + arrVals(j)Next
lngValUBound = lngFieldNamesUBound
If lngValUBound >= 0 ThenSet colVals = New CollectionEnd If
For j = 0 To lngValUBoundcolVals.Add Trim(arrVals(j)), Trim(arrFieldNames(j))Next
For j = lngValUBound + 1 To lngFieldNamesUBoundcolVals.Add "", Trim(arrFieldNames(j))Next
colRecs.Add colVals
Set GetCSVCollection = colRecs
2buck |
Latest page update: made by 2buck
, Dec 1 2006, 11:57 PM EST
(about this update
About This Update
Edited by 2buck
view changes - complete history) |
|
More Info: links to this page
|