A Microsoft Excel forum. ExcelBanter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » ExcelBanter forum » Excel Newsgroups » Excel Programming
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Read csv to 2D array



 
 
Thread Tools Display Modes
  #1  
Old October 21st 08, 08:50 PM posted to microsoft.public.excel.programming
Dave
external usenet poster
 
Posts: 1,389
Default Read csv to 2D array

How can I read in a csv (comma delimited) file into a 2D array in VBA?
Ads
  #2  
Old October 22nd 08, 07:47 AM posted to microsoft.public.excel.programming
joel
external usenet poster
 
Posts: 9,101
Default 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?

  #3  
Old October 23rd 08, 06:54 AM posted to microsoft.public.excel.programming
Tim Williams
external usenet poster
 
Posts: 1,588
Default 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?



 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
ADO read to and array Deke Excel Programming 4 June 24th 08 07:31 PM
Read a Range into an Array Paul Black Excel Programming 4 August 12th 07 07:47 PM
Excel, read in an array AustinJames Setting up and Configuration of Excel 4 September 20th 05 03:18 PM
Read a range to an array Microsoft Forum Excel Programming 4 January 23rd 05 05:23 PM
Read in Array Leigh Excel Programming 1 May 5th 04 09:18 AM


All times are GMT +1. The time now is 07:32 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Copyright ©2004-2013 ExcelBanter.
The comments are property of their posters.