Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 52
Default Parsing a string

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.
  #2   Report Post  
Posted to microsoft.public.excel.programming
Ed Ed is offline
external usenet poster
 
Posts: 399
Default Parsing a string

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

"simonc" wrote in message
...
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.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Parsing a string

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


"simonc" wrote in message
...
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.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 126
Default Parsing a string

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

"simonc" wrote in message
...
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.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 126
Default Parsing a string

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

"kounoike" wrote in message
...
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


Reply
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
Need help parsing a string Daminc[_38_] Excel Programming 4 January 26th 06 11:53 AM
String Parsing : Best Methods : VBA vmegha Excel Programming 1 January 23rd 06 02:25 PM
parsing a string Mark[_57_] Excel Programming 4 April 28th 05 04:42 PM
Parsing a String to get Numbers nabukhalaf[_2_] Excel Programming 4 October 6th 04 12:05 AM
Unicode string parsing? Please help! Douglas Gennetten[_2_] Excel Programming 1 January 16th 04 08:31 AM


All times are GMT +1. The time now is 12:19 PM.

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

About Us

"It's about Microsoft Excel"