View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
GS[_2_] GS[_2_] is offline
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