View Single Post
  #1   Report Post  
Old June 3rd 19, 10:41 AM posted to microsoft.public.excel.programming
RG III RG III is offline
external usenet poster
First recorded activity by ExcelBanter: May 2019
Posts: 64
Default Why the heck doesn't this work?

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

Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Function 'ReadTextFile()
Sub FlawedSub()
Dim vTmp, sLine$

Const sFile$ = "C:\data.txt"

sLine = ReadTextFile(sFile)
sLine = Trim(sLine)

vTmp = Split(Application.Substitute(sLine, " ", "*", 1), "*")

MsgBox Trim(vTmp(0))

End Sub

The file data.txt contains the following string:
(There's a tab char just after the a123)

a123 A. BC #123

The output is supposed to be "a123". Instead, I'm
getting "a123 A."

Ugh, why doesn't this work? BTW, I'm using Excel 2010,
so I don't know if that's the problem.

Is there another way to extract the first substring on the
nth line in a file?