ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Read csv to 2D array (https://www.excelbanter.com/excel-programming/418853-read-csv-2d-array.html)

Dave

Read csv to 2D array
 
How can I read in a csv (comma delimited) file into a 2D array in VBA?

joel

Read csv to 2D array
 
Sub EditInput()
Const ReadFile = "c:\temp\event.txt"

Const ForReading = 1, ForWriting = 2, _
ForAppending = 3

Dim Index As Integer
Dim data() As Variant

Set fs = CreateObject("Scripting.FileSystemObject")
Set fin = fs.OpenTextFile(ReadFile, _
ForReading, TristateFalse)

Index = 0
Do While fin.AtEndOfStream < True
ReadData = fin.readline
ReDim Preserve data(0 To 1, 0 To Index)
Splitdata = Split(ReadData, ",")
data(0, Index) = Splitdata(0)
data(1, Index) = Splitdata(1)
Index = Index + 1
Loop
fin.Close
End Sub


"Dave" wrote:

How can I read in a csv (comma delimited) file into a 2D array in VBA?


Tim Williams

Read csv to 2D array
 
Watch out for quoted values which may contain commas.

Tim

"Joel" wrote in message
...
Sub EditInput()
Const ReadFile = "c:\temp\event.txt"

Const ForReading = 1, ForWriting = 2, _
ForAppending = 3

Dim Index As Integer
Dim data() As Variant

Set fs = CreateObject("Scripting.FileSystemObject")
Set fin = fs.OpenTextFile(ReadFile, _
ForReading, TristateFalse)

Index = 0
Do While fin.AtEndOfStream < True
ReadData = fin.readline
ReDim Preserve data(0 To 1, 0 To Index)
Splitdata = Split(ReadData, ",")
data(0, Index) = Splitdata(0)
data(1, Index) = Splitdata(1)
Index = Index + 1
Loop
fin.Close
End Sub


"Dave" wrote:

How can I read in a csv (comma delimited) file into a 2D array in VBA?





All times are GMT +1. The time now is 05:13 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com