LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Excel VBA help: Text file formatting

Good stuff!!

Try this in a standard module...

Option Explicit

Sub Parse_ScanFile()
' Parses XY data from a scan file
Dim sFile$, vData, saDataOut$(), v1, v2
Dim n&, j&, k&, MaxCols&

sFile = Application.GetOpenFilename
If sFile = "False" Then Exit Sub '//user cancels
vData = Split(ReadTextFile(sFile), vbCrLf)

'Load the header row
ReDim Preserve saDataOut(j)
saDataOut(j) = "RET,Value1,Value2": j = j + 1

'Iterate each block of scan data
For n = 14 To UBound(vData) - 15 Step 15
v1 = Split(vData(n + 1), "= ")
v2 = Split(vData(n + 3), "= ")
If v2(1) = "10" Then
For k = 5 To 14
ReDim Preserve saDataOut(j)
saDataOut(j) = v1(1) & "," & vData(n + k): j = j + 1
Next 'k
End If 'v2="10"
Next 'n

'Transfer output data to a 2D 1-based array
vData = saDataOut: Erase saDataOut
MaxCols = UBound(Split(vData(0), ",")) + 1
ReDim saDataOut(1 To UBound(vData) + 2, 1 To MaxCols)
For n = LBound(vData) To UBound(vData)
v1 = Split(vData(n), ",")
For k = LBound(v1) To UBound(v1)
saDataOut(n + 1, k + 1) = v1(k)
Next 'k
Next 'n
'Dump the data
Cells(1, 1).Resize(UBound(saDataOut), MaxCols) = saDataOut
End Sub

Function ReadTextFile$(Filename$)
' Reads large amounts of data from a text file in one single step.
Dim iNum%
On Error GoTo ErrHandler
iNum = FreeFile(): Open Filename For Input As #iNum
ReadTextFile = Space$(LOF(iNum))
ReadTextFile = Input(LOF(iNum), iNum)

ErrHandler:
Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Function 'ReadTextFile()

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion




 
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
formatting a text file imported into excel GinaH Excel Worksheet Functions 1 August 6th 09 08:30 PM
How to force column formatting (text, date, etc) when loading a fixed width text file into an array? ker_01 Excel Programming 3 October 21st 08 08:45 PM
Some Text formatting may have changed in this file because Roland Excel Discussion (Misc queries) 0 September 18th 08 02:21 PM
some text formatting may have changed in this file because the max peng peng Excel Discussion (Misc queries) 2 July 23rd 08 08:00 AM
excel formatting to text file paul Setting up and Configuration of Excel 4 July 26th 05 03:17 AM


All times are GMT +1. The time now is 06:02 PM.

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

About Us

"It's about Microsoft Excel"