Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Public Function basCSV2Array(FName As String, _
Optional RecSep As String = vbCrLf, _
Optional FldSep As String = ",") As Variant
Dim Fil As Integer
Dim NRecs As Long
Dim NFlds As Long
Dim Idx As Long
Dim Jdx As Integer
Dim J As Long 'indices
Dim RawFile As String 'Holds the entire contents of the file
Dim RawSplit() As String 'the file split up on a line per line basis
Dim OneLine() As String 'used to hold the tokens for one line
Dim RptAry() As String '2d array. Holds X lines & N elements per line
Fil = FreeFile 'get the next free file number
'This works for large files. I (Troy Williams)tried it
'with a 50 meg file on a computer with 128 Mb of ram and it worked fine.
'open the file and dump the contents into the rawfile variable
Open FName For Binary As #Fil 'Open file
RawFile = String$(LOF(Fil), 32) 'Create "empty" String of Length
Get #Fil, 1, RawFile 'Fill "Empty Str with File
Close #Fil 'Close File
'Get the Nunber of Records and Fields
RawSplit = Split(RawFile, RecSep) 'Split the file up by lines
NRecs = UBound(RawSplit) - 1 'Num Lines/Records in the file
OneLine = Split(RawSplit(0), FldSep) 'Split the first line
NFlds = UBound(OneLine) 'Get the Number of Fields
ReDim RptAry(0 To NRecs, 0 To NFlds)
For Idx = 0 To NRecs
OneLine = Split(RawSplit(Idx), FldSep)
For Jdx = 0 To NFlds
RptAry(Idx, Jdx) = OneLine(Jdx)
Next Jdx
Next Idx
basCSV2Array = RptAry
End Function
Public Function basTstCSV2Array()
Dim Fil As Integer
Dim Idx As Long
Dim Jdx As Long
Dim MyLOF As Long
Dim FilNme As String
Dim MyArray As Variant
FilNme = "C:\My Documents\MyCsvFile.Txt"
Fil = FreeFile
Open FilNme For Binary As #Fil 'Open file
MyLOF = LOF(Fil)
Close #Fil 'Close File
MyArray = basCSV2Array(FilNme, , " ")
Idx = UBound(MyArray, 1)
Jdx = UBound(MyArray, 2)
' For Kdx = 0 To Idx
' For Ldx = 0 To Jdx
' Debug.Print MyArray(Kdx, Ldx);
' Next Ldx
' Debug.Print
' Next Kdx
Debug.Print "# Lines = " & UBound(MyArray, 1) + 1
Debug.Print "# Words = " & (UBound(MyArray, 1) + 1) * (UBound(MyArray, 2) + 1)
Debug.Print "# Chars = " & MyLOF - ((UBound(MyArray, 1) + 1) * (UBound(MyArray, 2) + 1))
'? basTstCSV2Array
'# Lines = 2
'# Words = 16
'# Chars = 90
End Function
123456, 08/15/2001, 2:39:40 AM, 2:41:29 AM, 49 secs
123654, 08/15/2001, 2:49:40 AM, 2:51:29 AM, 49 secs