Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Read data from ascii file

I'd like to read a ascii file using VBA and the file has
(for example):
------------------------------------------------
Name Group Measured Modelled
d311 obsgroup 0.3270859 0.2853140
d312 obsgroup 0.3436139 0.3403970
--------------------------------------------------------
I have written:
dim dataname, groupname as string
Dim meas, modell as double

Line Input #1, textline ' Read text line
Do While Not EOF(1) ' Loop until end of file.
Input #1, dataname, groupname, meas, modell
Count = Count + 1
Loop
*******
But the variables does not read correctly. Could you help
me?

Nazrul
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Read data from ascii file

Iinput# is designed to read comma delimited data. You data is not comma
delimited.

You will need to use Line Input into a text string and parse it.

You could do this with the split statement if you are using xl2000 or later.

--
Regards,
Tom Ogilvy

"Nazrul" wrote in message
...
I'd like to read a ascii file using VBA and the file has
(for example):
------------------------------------------------
Name Group Measured Modelled
d311 obsgroup 0.3270859 0.2853140
d312 obsgroup 0.3436139 0.3403970
--------------------------------------------------------
I have written:
dim dataname, groupname as string
Dim meas, modell as double

Line Input #1, textline ' Read text line
Do While Not EOF(1) ' Loop until end of file.
Input #1, dataname, groupname, meas, modell
Count = Count + 1
Loop
*******
But the variables does not read correctly. Could you help
me?

Nazrul



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default Read data from ascii file

Hi Nazrul,

The Input # statement is looking for a data layout like this:

"d311", "obsgroup", 0.3270859, 0.2853140

where text strings are quoted and each value is separated by a comma. If
the data file is set up like that, you could use code similar to this to
read it in:

Private Type muData
Name As String
Group As String
Measured As Double
Modelled As Double
End Type

Sub GetData(rsFilePath As String)
Dim nFile As Integer
Dim sDummy As String
Dim uData() As muData
Dim sMeasured As String
Dim sModelled As String
Dim lRecord As Long

nFile = FreeFile
Open rsFilePath For Input Shared As #nFile

Line Input #nFile, sDummy
Do While Not EOF(nFile)
ReDim Preserve uData(lRecord)
With uData(lRecord)
Input #nFile, .Name, .Group, .Measured, .Modelled
End With
lRecord = lRecord + 1
Loop
Stop
Close #nFile
End Sub

You'd of course want to add error handling to trap for invalid file name or
format.

If your file is going to remain in the format you specified, you could read
each line in with Line Input # and parse the string with the Mid$()
function, assigning each part to the appropriate variable. Another
alternative is to use the FileSystemObject (part of the Scripting Runtime
library), which encapsulates a lot of this file I/O stuff and adds some nice
functionality. Search MSDN for "FileSystemObject" for more information on
that.

--
Regards,

Jake Marx
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]


Nazrul wrote:
I'd like to read a ascii file using VBA and the file has
(for example):
------------------------------------------------
Name Group Measured Modelled
d311 obsgroup 0.3270859 0.2853140
d312 obsgroup 0.3436139 0.3403970
--------------------------------------------------------
I have written:
dim dataname, groupname as string
Dim meas, modell as double

Line Input #1, textline ' Read text line
Do While Not EOF(1) ' Loop until end of file.
Input #1, dataname, groupname, meas, modell
Count = Count + 1
Loop
*******
But the variables does not read correctly. Could you help
me?

Nazrul


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
Merge data from an ascii file into a Word doc Bernie Excel Discussion (Misc queries) 1 September 6th 09 06:54 AM
How do I convert an Excel file to an ASCII file? Pugh Excel Discussion (Misc queries) 1 November 8th 06 08:57 PM
using data from ascii file? NTaylor Excel Discussion (Misc queries) 2 December 21st 05 04:46 PM
How do I convert excel file into ASCII text file with alignment? Rosaiah Excel Discussion (Misc queries) 2 June 27th 05 12:17 PM
Selective read data from ascii file Steve Kim Excel Programming 0 August 20th 03 05:19 AM


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