Parsing a string

S

simonc

I am reading lines of a text file, which has values in many columns, and
dividing these into their elements by writing them one by one in a cell in a
worksheet and using texttocolumns with space as the delimiter and treating
consecutive delimiters as one. I don't need the values in the worksheet, and
once I've extracted the particular elements I need I write these to another
text file.

This seems to take a while so I wondered if there was a quicker way of doing
it in code, without using a worksheet. There is the split function, but I
can't find a way of getting it to treat consecutive spaces as a single
delimiter. Is there a way?

I am using Excel 2000.

Grateful for any ideas.
 
E

Ed

Sounds like a "table" created by using spaces in a plain text document. If
that's the case, I have a macro I use in Word that allows me to select the
"plain text table" and make it into a Word table. This can then be copied
directly in Excel. Would this help?

Ed
 
B

Bernie Deitrick

Siman,

As long as they are really spaces:

Dim myString As String
Dim blah As Variant
Dim i As Integer

myString = "hello there"
blah = Split(Application.WorksheetFunction.Trim(myString), " ")
For i = LBound(blah) To UBound(blah)
MsgBox blah(i)
Next i

HTH,
Bernie
MS Excel MVP
 
K

kounoike

Hi
i don't know this would meet your demand and don't think a quicker way
of doing.
assume a original file name is hoge.txt, then this would make a file
named as hoge_temp.txt in the same directory as the original file.

Sub parsefile2()
Dim rec
Dim filename As String, ftmp As String
Dim Fin As Integer, Fout As Integer
Dim WorkResult As String, tmp As String
Dim i As Long

On Error GoTo ErrorCheck
filename = Application.GetOpenFilename(FileFilter:="All File
(*.*),*")
If filename = "False" Then
Exit Sub
End If

pos = InStrRev(filename, ".")
If pos > 0 Then
ftmp = Left(filename, pos - 1) & "_temp" & Mid(filename, pos)
Else
ftmp = filename & "_temp"
End If
Application.ScreenUpdating = False
Application.EnableEvents = False
Fin = FreeFile()
Open filename For Input As #Fin
Fout = FreeFile()
Open ftmp For Output As #Fout
Application.ScreenUpdating = False
Do While Not EOF(Fin)
Line Input #Fin, WorkResult
tmp = ""
rec = Split(WorkResult, Space(1))
For i = LBound(rec) To UBound(rec)
If rec(i) <> "" Then
tmp = tmp & rec(i) & Space(1)
End If
Next
tmp = Trim(tmp) & Chr(13) & Chr(10)
Print #Fout, tmp;
Loop
Application.StatusBar = False
Application.EnableEvents = True
Application.ScreenUpdating = True
Close #Fin
Close #Fout
Exit Sub
ErrorCheck:
Application.StatusBar = False
Application.EnableEvents = True
Application.ScreenUpdating = True
Close #Fin
Close #Fout
MsgBox "An error occured in the code."
End Sub

keizi
 
K

kounoike

I'm very sorry. This macro doesn't work correctly. Ignore my post.

kounoike said:
Hi
i don't know this would meet your demand and don't think a quicker way
of doing.
assume a original file name is hoge.txt, then this would make a file
named as hoge_temp.txt in the same directory as the original file.

Sub parsefile2()
Dim rec
Dim filename As String, ftmp As String
Dim Fin As Integer, Fout As Integer
Dim WorkResult As String, tmp As String
Dim i As Long

On Error GoTo ErrorCheck
filename = Application.GetOpenFilename(FileFilter:="All File
(*.*),*")
If filename = "False" Then
Exit Sub
End If

snip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top