Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Reading Text file into an array

I want to read an ASCII text file that contains variable length lines
into an array. I can read the file a line at a time or a character at
a time but what I really need is to read the text between delimiters
as a unit. Is there an end of line marker (similar to the EOF
marker)? How do I capture the information between delimiters?

TIA
Garry

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Reading Text file into an array

Read your whole text file and put it in a string variable.
Then split the string with the Split function of whatever
delimiter you choose.

RBS

wrote in message
oups.com...
I want to read an ASCII text file that contains variable length lines
into an array. I can read the file a line at a time or a character at
a time but what I really need is to read the text between delimiters
as a unit. Is there an end of line marker (similar to the EOF
marker)? How do I capture the information between delimiters?

TIA
Garry


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Reading Text file into an array

the end of line marker in windows is two characters together. In VBA is is
represented with the constant: vbCrLf

But that would be the same as reading it in a line at a time which you say
you can do.
If you actually want to parse a delimited file, then Chip Pearson has sample
code for that:

http://www.cpearson.com/excel/imptext.htm import/export text files

--
Regards,
Tom Ogilvy




" wrote:

I want to read an ASCII text file that contains variable length lines
into an array. I can read the file a line at a time or a character at
a time but what I really need is to read the text between delimiters
as a unit. Is there an end of line marker (similar to the EOF
marker)? How do I capture the information between delimiters?

TIA
Garry


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Reading Text file into an array

On Apr 18, 11:52 am, "RB Smissaert"
wrote:
Read your whole text file and put it in a string variable.
Then split the string with the Split function of whatever
delimiter you choose.

RBS

wrote in message

oups.com...



I want to read an ASCII text file that contains variable length lines
into an array. I can read the file a line at a time or a character at
a time but what I really need is to read the text between delimiters
as a unit. Is there an end of line marker (similar to the EOF
marker)? How do I capture the information between delimiters?


TIA
Garry- Hide quoted text -


- Show quoted text -


I'll give that a shot. To load the array how do I locate the end of
the string in which I have placed the file info?

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Reading Text file into an array

Get the file into a string variable with something like this:

Function OpenTextFileToString(strFile As String) As String

Dim hFile As Long

'obtain file handle, open file
'and load into a string buffer
hFile = FreeFile

Open strFile For Input As #hFile

OpenTextFileToString = Input$(LOF(hFile), hFile)

Close #hFile

End Function


Then you can do something like this:

Sub test()

Dim strString As String
Dim arr

strString = OpenTextFileToString("C:\test.txt")

arr = Split(strString, vbCrLf)

End Sub


Replace the vbCrLf with whatever your delimiter is.


RBS




wrote in message
ups.com...
On Apr 18, 11:52 am, "RB Smissaert"
wrote:
Read your whole text file and put it in a string variable.
Then split the string with the Split function of whatever
delimiter you choose.

RBS

wrote in message

oups.com...



I want to read an ASCII text file that contains variable length lines
into an array. I can read the file a line at a time or a character at
a time but what I really need is to read the text between delimiters
as a unit. Is there an end of line marker (similar to the EOF
marker)? How do I capture the information between delimiters?


TIA
Garry- Hide quoted text -


- Show quoted text -


I'll give that a shot. To load the array how do I locate the end of
the string in which I have placed the file info?




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Reading Text file into an array

On Apr 18, 1:25 pm, "RB Smissaert"
wrote:
Get the file into a string variable with something like this:

Function OpenTextFileToString(strFile As String) As String

Dim hFile As Long

'obtain file handle, open file
'and load into a string buffer
hFile = FreeFile

Open strFile For Input As #hFile

OpenTextFileToString = Input$(LOF(hFile), hFile)

Close #hFile

End Function

Then you can do something like this:

Sub test()

Dim strString As String
Dim arr

strString = OpenTextFileToString("C:\test.txt")

arr = Split(strString, vbCrLf)

End Sub

Replace the vbCrLf with whatever your delimiter is.

RBS

wrote in message

ups.com...



On Apr 18, 11:52 am, "RB Smissaert"
wrote:
Read your whole text file and put it in a string variable.
Then split the string with the Split function of whatever
delimiter you choose.


RBS


wrote in message


groups.com...


I want to read an ASCII text file that contains variable length lines
into an array. I can read the file a line at a time or a character at
a time but what I really need is to read the text between delimiters
as a unit. Is there an end of line marker (similar to the EOF
marker)? How do I capture the information between delimiters?


TIA
Garry- Hide quoted text -


- Show quoted text -


I'll give that a shot. To load the array how do I locate the end of
the string in which I have placed the file info?- Hide quoted text -


- Show quoted text -


Excellent. I used the tab (Chr(9)) which is the delimiter for this
ASCII file and it worked great. Thanks for the good advice.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading from a text file Jeff Excel Discussion (Misc queries) 1 November 8th 06 08:47 PM
Reading a text file ? WTG Excel Worksheet Functions 2 February 22nd 05 01:29 AM
Reading a text file WTG Excel Discussion (Misc queries) 2 February 22nd 05 01:27 AM
Help reading in a text file Mike[_60_] Excel Programming 1 January 21st 04 06:29 PM
Reading .csv file into array mousetrap Excel Programming 1 November 26th 03 07:47 AM


All times are GMT +1. The time now is 12:33 PM.

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

About Us

"It's about Microsoft Excel"