ich möchte eine CSV-Datei einlesen und habe mir dafür folgenden Code im Netz gesucht, der den Inhalt der csv in ein Array schreiben sollte:
Public Sub import_MS_file()
Dim file_name As String
Dim fnum As Integer
Dim whole_file As String
Dim lines As Variant
Dim one_line As Variant
Dim num_rows As Long
Dim num_cols As Long
Dim the_array() As String
Dim R As Long
Dim C As Long
file_name = Application.GetOpenFilename("csv-Datei (*.csv),*.csv")
'Load the file
fnum = FreeFile
Open file_name For Input As fnum
whole_file = Input$(LOF(fnum), #fnum)
Close fnum
'Break the file into lines
lines = Split(whole_file, vbCrLf)
'Dimension the array
ReDim the_array(239, 17)
'Copy the data into the array
For R = 0 To 238
one_line = Split(lines(R), ";")
If UBound(one_line) -1 Then
For C = 0 To UBound(one_line)
the_array(R, C) = one_line(C)
Next C
End If
Next R
End Sub
Die csv Datei hat 239 Zeilen und ich will maximal 17 Spalten einlesen. Bei der Zeilethe_array(R, C) = one_line(C)
wird dann immer ein Fehler angezeigt, und zwar soll der Index außerhalb des gültigen Bereiches liegen. Manche der Zeilen sind Leerzeilen, manche auch kürzer als 17 Spalten. Wie kann ich diesen Fehler umgehen?
Viele Grüße und Danke im Voraus,
Peter