Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 52
Default Split with consecutive delimiters

I am reading lines from a formatted data file (values in right justified
columns) and using split to extract the values from each line into an array.
The problem is the number of spaces between values is not constant, and split
adds a new array value for every space it finds in the text string. Is there
a way to set split to treat consecutive spaces as a single delimiter like you
can in TextToColumns?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Split with consecutive delimiters

Do you mean that you actually have a fixed-width format file ?

NickHK

"simonc" wrote in message
...
I am reading lines from a formatted data file (values in right justified
columns) and using split to extract the values from each line into an

array.
The problem is the number of spaces between values is not constant, and

split
adds a new array value for every space it finds in the text string. Is

there
a way to set split to treat consecutive spaces as a single delimiter like

you
can in TextToColumns?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Split with consecutive delimiters

The following removes multiple spaces from a cell:

Sub break_up()
''''''''''''''''''''''''''''''''''''''''''''
' converts multiple spaces to a single space
''''''''''''''''''''''''''''''''''''''''''''
Dim v As String
Dim v_out As String
v = Selection.Value
v_out = ""
c = 0
For i = 1 To Len(v)
vv = Mid(v, i, 1)
If vv = " " Then
c = c + 1
Else
Select Case c
Case 0
v_out = v_out & vv
Case 1
v_out = v_out & " " & vv
Case Is 1
v_out = v_out & " " & vv
End Select
c = 0
End If
Next
Selection.Value = v_out
End Sub

you can adapt the logic to remove multiple spaces from your string before
"splitting" it.
--
Gary''s Student - gsnu200721
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Split with consecutive delimiters

Ignore my previous post. TRIM is available to VBA:

Sub single_spaces()
Selection.Value = Application.WorksheetFunction.Trim(Selection.Value )
End Sub

--
Gary''s Student - gsnu200721
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Split with consecutive delimiters

On Wed, 16 May 2007 02:21:02 -0700, simonc
wrote:

I am reading lines from a formatted data file (values in right justified
columns) and using split to extract the values from each line into an array.
The problem is the number of spaces between values is not constant, and split
adds a new array value for every space it finds in the text string. Is there
a way to set split to treat consecutive spaces as a single delimiter like you
can in TextToColumns?


No but you could remove the extraneous spaces by using
application.worksheetfunction.trim on the string. (Do NOT use the VBA Trim
function -- it only removes leading and trailing spaces).


--ron


  #6   Report Post  
Posted to microsoft.public.excel.programming
Tim Tim is offline
external usenet poster
 
Posts: 145
Default Split with consecutive delimiters

Split seesm like the wrong approach.
What if there are empty values in the data ?

You might be better off creating a function which returns a specified field
from the line (and that can also trim off any leading spaces).
How you would construct this would depend on how many fields there are, but
a simple select case would probably be fine

(untested)

Function GetField(fnum as integer,sLine as string)
dim rv as string
select case fnum
case 1: rv=left(sLine,10)
case 2:rv=mid(sline,11,10)
case 3:rv=mid(sline,21,15)
'...etc
case else: rv=""
end select

GetField=trim(rv)

end function


Tim

"simonc" wrote in message
...
I am reading lines from a formatted data file (values in right justified
columns) and using split to extract the values from each line into an
array.
The problem is the number of spaces between values is not constant, and
split
adds a new array value for every space it finds in the text string. Is
there
a way to set split to treat consecutive spaces as a single delimiter like
you
can in TextToColumns?



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
Multiple Delimiters Doug Mc New Users to Excel 9 October 18th 09 03:36 PM
Multiple Delimiters hassanq23 Excel Discussion (Misc queries) 5 April 7th 09 01:16 AM
Consecutive date range on consecutive worksheets john3478 Excel Worksheet Functions 3 January 14th 09 10:54 PM
delimiters and manipulation PBArich Excel Worksheet Functions 0 June 12th 07 06:46 PM
How Do I Modify Pearson's Code to Add "Treat Consecutive Delimiters As One"? [email protected] Excel Programming 4 June 14th 06 12:23 AM


All times are GMT +1. The time now is 05:23 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"