I have a .csv file that looks like this:
Unit,,PU,,,,,Start Date & Time,,,5/20/2020 17:32,,
Name,,PNR15.5,,,,,,,,,,
ID,,TEST52020,,,,,End Date & Time,,,,,
No.,,77,,,,,,,,,,
,,,,,,,,,,,,
Phase name,,Start Time,,End Time,,,,,,,,
C,,,,,,,,,,,,
S,,,,,,,,,,,,
I am trying to convert it to a 2-dimensional array, in Excel VBA memory, that can be referenced by Excel cells.
There are hundreds of CSV files.
I tried line by line, but element2(k,l) line gives me an error.
Dim ArrayOfElements As Variant
Dim line, element, element2() As Variant
Dim k, l as Integer
k=0
Do While filename <> vbNullString
Open path & filename For Input As #1
Do While Not EOF(1)
Line Input #1, line
ArrayOfElements = Split(line, ",")
k = k + 1
l = 1
For Each element In ArrayOfElements
element2(k, l) = element
l = l + 1
Next
Loop
Close #1
Loop
Eventually, I want: Cells(1,1).Value = element2(1,1).

element2()before you begin populating it. The easier way is to open the CSV in Excel, and read the data from the worksheet. Then you don't need to worry about how to correctly parse the CSV format (such as how to handle field values with embedded spaces for example)Workbooks.OpenTextor a Query and then read it into a vba array in a single step (e.gmyArr = UsedRange) and then delete the sheet.Redim Preserve, but the issue is you can only resize the last dimension (so you can't add more "rows")