Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
hmm hmm is offline
external usenet poster
 
Posts: 175
Default Macro function to read text file

I am looking for a macro function that will read tab-separted pairs of x-y
data from a text file. For example, it could be ReadLine(filename,
line_number), which would return a line of text that I can manipulate using
worksheet functions to get the x and y values. It could, but does not have
to be, smarter, and return actual x and y values, i.e, {ReadPoint(filename,
line_number)}, a two-element array returning x and y values. This is all for
the purpose of eventually charting the data.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Macro function to read text file

Right from the macro Recorder:

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 9/16/2007 by jim ravenswood
'

'
ChDir "C:\"
Workbooks.OpenText Filename:="C:\sample.txt", Origin:=437, StartRow:=1, _
DataType:=xlDelimited, TextQualifier:=xlDoubleQuote,
ConsecutiveDelimiter _
:=False, Tab:=True, Semicolon:=False, Comma:=False, Space:=False, _
Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1)), _
TrailingMinusNumbers:=True
End Sub


By using the Recorder, you can capture all the information you supply the
Import Wizard manually.
--
Gary''s Student - gsnu200745


"hmm" wrote:

I am looking for a macro function that will read tab-separted pairs of x-y
data from a text file. For example, it could be ReadLine(filename,
line_number), which would return a line of text that I can manipulate using
worksheet functions to get the x and y values. It could, but does not have
to be, smarter, and return actual x and y values, i.e, {ReadPoint(filename,
line_number)}, a two-element array returning x and y values. This is all for
the purpose of eventually charting the data.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Macro function to read text file

If you want to read the values in VBA without using the worksheet then use
this code

Sub TextStreamTest()
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fs, f, ts, s


Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile("c:\temp\abc.txt")
Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
Do While ts.atendofstream < True
InputLine = ts.ReadLine
x = Val(Left(InputLine, _
InStr(InputLine, ",") - 1))
y = Val(Mid(InputLine, _
InStr(InputLine, ",") + 1))
Loop
ts.Close
End Sub

"Gary''s Student" wrote:

Right from the macro Recorder:

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 9/16/2007 by jim ravenswood
'

'
ChDir "C:\"
Workbooks.OpenText Filename:="C:\sample.txt", Origin:=437, StartRow:=1, _
DataType:=xlDelimited, TextQualifier:=xlDoubleQuote,
ConsecutiveDelimiter _
:=False, Tab:=True, Semicolon:=False, Comma:=False, Space:=False, _
Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1)), _
TrailingMinusNumbers:=True
End Sub


By using the Recorder, you can capture all the information you supply the
Import Wizard manually.
--
Gary''s Student - gsnu200745


"hmm" wrote:

I am looking for a macro function that will read tab-separted pairs of x-y
data from a text file. For example, it could be ReadLine(filename,
line_number), which would return a line of text that I can manipulate using
worksheet functions to get the x and y values. It could, but does not have
to be, smarter, and return actual x and y values, i.e, {ReadPoint(filename,
line_number)}, a two-element array returning x and y values. This is all for
the purpose of eventually charting the data.

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
How do you save an excel file to be read as IBM-type text file ? Dee Franklin Excel Worksheet Functions 2 October 10th 06 02:46 AM
how to create a macro/vba to read an text file and filled colluns on excel+vloockp EHM Excel Programming 1 October 25th 04 05:03 PM
read text file hallie Excel Programming 2 August 17th 04 01:00 PM
read text file hallie Excel Programming 1 August 17th 04 12:37 PM
Read text file jacob[_3_] Excel Programming 2 September 23rd 03 06:41 PM


All times are GMT +1. The time now is 11:23 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"