Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Merge data from an ascii file into a Word doc | Excel Discussion (Misc queries) | |||
How do I convert an Excel file to an ASCII file? | Excel Discussion (Misc queries) | |||
using data from ascii file? | Excel Discussion (Misc queries) | |||
How do I convert excel file into ASCII text file with alignment? | Excel Discussion (Misc queries) | |||
Selective read data from ascii file | Excel Programming |