Load csv file into a VBA array rather than Excel Sheet

前端 未结 6 1108
半阙折子戏
半阙折子戏 2020-11-28 07:25

I am currently able to enter csv file data into Excel VBA by uploading the data via the code below then handling the table, surely not the best way as I am only interested i

6条回答
  •  無奈伤痛
    2020-11-28 08:00

    Alternatively you can use a code like this

    Dim line As String, Arr
    Dim FSO As Object, Fo As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set Fo = FSO.OpenTextFile("csvfile.csv")
    While Not Fo.AtEndOfStream
     line = Fo.ReadLine      ' Read the csv file line by line
     Arr = Split(line, ",")  ' The csv line is loaded into the Arr as an array
     For i = 0 To UBound(Arr) - 1: Debug.Print Arr(i) & " ";: Next
     Debug.Print
    Wend
    
     01/01/2019 1 1 1 36 55.6 0.8 85.3 95 95 109 102 97 6 2.5 2.5 3.9 
     01/01/2019 1 2 0 24 0.0 2.5 72.1 89 0 0 97 95 10 6.7 4.9 3.9 
     01/01/2019 1 3 1 36 26.3 4 80.6 92 92 101 97 97 8 5.5 5.3 3.7 
     01/01/2019 1 4 0 16 30.0 8 79.2 75 74 87 87 86 10 3.8 4 4.2 
    

提交回复
热议问题