PC Review


Reply
Thread Tools Rate Thread

How to compress interstitial spaces?

 
 
Joe User
Guest
Posts: n/a
 
      23rd Dec 2009
In VBA, I want to replace multiple interstitial spaces with one space in a
string variable. For example, " a b c d e f g " should
become "a b c d e f g".

How can I do this using just standard VBA features? (I want to avoid adding
References.)

The following is close to what I want:

Dim mylist as String
[....]
mylist = Replace(Trim(mylist)," "," ")

The problem is: apparently Replace starts scanning again after the
replacement text. Consequently, for example, 4 spaces are reduced to 2
spaces instead of 1.

I 'spose I could do something like:

mylist = Trim(mylist)
Do
mylist0 = mylist
mylist = Replace(mylist," "," ")
Loop until mylist = mylist0

That works. But is there something better?

I would like an answer to the question above, for my edification.

But my purpose is to compress spaces before using Split(mylist) to separate
the "words".

Is there a way to do the Split without having to compress multiple spaces
first?

I am using Excel 2003 SP3 with VBA 6.5.1024.

 
Reply With Quote
 
 
 
 
Don Guillett
Guest
Posts: n/a
 
      24th Dec 2009
One way to replace the extra internal spaces is to delete all and put back
one between each letter. This won't work for aa b ccc d

Option Explicit
Sub trimextraspaces()
Dim i As Double
Dim ms As String
With ActiveCell
..Replace " ", ""
For i = 1 To Len(.Value)
ms = ms & Mid(.Value, i, 1) & " "
Next i
..Value = ms
End With
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software
(E-Mail Removed)
"Joe User" <joeu2004> wrote in message
news:%(E-Mail Removed)...
> In VBA, I want to replace multiple interstitial spaces with one space in a
> string variable. For example, " a b c d e f g "
> should become "a b c d e f g".
>
> How can I do this using just standard VBA features? (I want to avoid
> adding References.)
>
> The following is close to what I want:
>
> Dim mylist as String
> [....]
> mylist = Replace(Trim(mylist)," "," ")
>
> The problem is: apparently Replace starts scanning again after the
> replacement text. Consequently, for example, 4 spaces are reduced to 2
> spaces instead of 1.
>
> I 'spose I could do something like:
>
> mylist = Trim(mylist)
> Do
> mylist0 = mylist
> mylist = Replace(mylist," "," ")
> Loop until mylist = mylist0
>
> That works. But is there something better?
>
> I would like an answer to the question above, for my edification.
>
> But my purpose is to compress spaces before using Split(mylist) to
> separate the "words".
>
> Is there a way to do the Split without having to compress multiple spaces
> first?
>
> I am using Excel 2003 SP3 with VBA 6.5.1024.


 
Reply With Quote
 
Charabeuh
Guest
Posts: n/a
 
      24th Dec 2009
Hello,

Try this in your VBA code:

mylist = Application.Trim(mylist)

' in this syntax Trim is the Excel function TRIM
' and not the VBA function TRIM.
' The excel function TRIM should do what you aim to do

Bye



"Joe User" <joeu2004> a écrit dans le message de groupe de discussion :
#(E-Mail Removed)...
> In VBA, I want to replace multiple interstitial spaces with one space in a
> string variable. For example, " a b c d e f g "
> should become "a b c d e f g".
>
> How can I do this using just standard VBA features? (I want to avoid
> adding References.)
>
> The following is close to what I want:
>
> Dim mylist as String
> [....]
> mylist = Replace(Trim(mylist)," "," ")
>
> The problem is: apparently Replace starts scanning again after the
> replacement text. Consequently, for example, 4 spaces are reduced to 2
> spaces instead of 1.
>
> I 'spose I could do something like:
>
> mylist = Trim(mylist)
> Do
> mylist0 = mylist
> mylist = Replace(mylist," "," ")
> Loop until mylist = mylist0
>
> That works. But is there something better?
>
> I would like an answer to the question above, for my edification.
>
> But my purpose is to compress spaces before using Split(mylist) to
> separate the "words".
>
> Is there a way to do the Split without having to compress multiple spaces
> first?
>
> I am using Excel 2003 SP3 with VBA 6.5.1024.


 
Reply With Quote
 
Joe User
Guest
Posts: n/a
 
      24th Dec 2009
"Charabeuh" <(E-Mail Removed)> wrote:
> mylist = Application.Trim(mylist)


Good. Thanks.

What's the difference, if any, between that and:

mylist = WorksheetFunction.Trim(mylist)

I know they are the same functionally. My question is: is there any reason
to use one form instead of the other?


----- original message -----

"Charabeuh" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hello,
>
> Try this in your VBA code:
>
> mylist = Application.Trim(mylist)
>
> ' in this syntax Trim is the Excel function TRIM
> ' and not the VBA function TRIM.
> ' The excel function TRIM should do what you aim to do
>
> Bye
>
>
>
> "Joe User" <joeu2004> a écrit dans le message de groupe de discussion :
> #(E-Mail Removed)...
>> In VBA, I want to replace multiple interstitial spaces with one space in
>> a string variable. For example, " a b c d e f g "
>> should become "a b c d e f g".
>>
>> How can I do this using just standard VBA features? (I want to avoid
>> adding References.)
>>
>> The following is close to what I want:
>>
>> Dim mylist as String
>> [....]
>> mylist = Replace(Trim(mylist)," "," ")
>>
>> The problem is: apparently Replace starts scanning again after the
>> replacement text. Consequently, for example, 4 spaces are reduced to 2
>> spaces instead of 1.
>>
>> I 'spose I could do something like:
>>
>> mylist = Trim(mylist)
>> Do
>> mylist0 = mylist
>> mylist = Replace(mylist," "," ")
>> Loop until mylist = mylist0
>>
>> That works. But is there something better?
>>
>> I would like an answer to the question above, for my edification.
>>
>> But my purpose is to compress spaces before using Split(mylist) to
>> separate the "words".
>>
>> Is there a way to do the Split without having to compress multiple spaces
>> first?
>>
>> I am using Excel 2003 SP3 with VBA 6.5.1024.

>


 
Reply With Quote
 
Don Guillett
Guest
Posts: n/a
 
      24th Dec 2009
It's what I should have suggested. Look in the help index vba for trim and
then look in the excel help index for trim to see the diff.

--
Don Guillett
Microsoft MVP Excel
SalesAid Software
(E-Mail Removed)
"Joe User" <joeu2004> wrote in message
news:(E-Mail Removed)...
> "Charabeuh" <(E-Mail Removed)> wrote:
>> mylist = Application.Trim(mylist)

>
> Good. Thanks.
>
> What's the difference, if any, between that and:
>
> mylist = WorksheetFunction.Trim(mylist)
>
> I know they are the same functionally. My question is: is there any
> reason to use one form instead of the other?
>
>
> ----- original message -----
>
> "Charabeuh" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> Hello,
>>
>> Try this in your VBA code:
>>
>> mylist = Application.Trim(mylist)
>>
>> ' in this syntax Trim is the Excel function TRIM
>> ' and not the VBA function TRIM.
>> ' The excel function TRIM should do what you aim to do
>>
>> Bye
>>
>>
>>
>> "Joe User" <joeu2004> a écrit dans le message de groupe de discussion :
>> #(E-Mail Removed)...
>>> In VBA, I want to replace multiple interstitial spaces with one space in
>>> a string variable. For example, " a b c d e f g "
>>> should become "a b c d e f g".
>>>
>>> How can I do this using just standard VBA features? (I want to avoid
>>> adding References.)
>>>
>>> The following is close to what I want:
>>>
>>> Dim mylist as String
>>> [....]
>>> mylist = Replace(Trim(mylist)," "," ")
>>>
>>> The problem is: apparently Replace starts scanning again after the
>>> replacement text. Consequently, for example, 4 spaces are reduced to 2
>>> spaces instead of 1.
>>>
>>> I 'spose I could do something like:
>>>
>>> mylist = Trim(mylist)
>>> Do
>>> mylist0 = mylist
>>> mylist = Replace(mylist," "," ")
>>> Loop until mylist = mylist0
>>>
>>> That works. But is there something better?
>>>
>>> I would like an answer to the question above, for my edification.
>>>
>>> But my purpose is to compress spaces before using Split(mylist) to
>>> separate the "words".
>>>
>>> Is there a way to do the Split without having to compress multiple
>>> spaces first?
>>>
>>> I am using Excel 2003 SP3 with VBA 6.5.1024.

>>

>


 
Reply With Quote
 
Charabeuh
Guest
Posts: n/a
 
      24th Dec 2009
I think your form is better than mine since TRIM is a member of the
WorksheetFunction object

The complete syntax in the object model should be:
mylist = Application.WorksheetFunction.Trim(mylist)



"Joe User" <joeu2004> a écrit dans le message de groupe de discussion :
(E-Mail Removed)...
> "Charabeuh" <(E-Mail Removed)> wrote:
>> mylist = Application.Trim(mylist)

>
> Good. Thanks.
>
> What's the difference, if any, between that and:
>
> mylist = WorksheetFunction.Trim(mylist)
>
> I know they are the same functionally. My question is: is there any
> reason to use one form instead of the other?
>
>
> ----- original message -----
>
> "Charabeuh" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> Hello,
>>
>> Try this in your VBA code:
>>
>> mylist = Application.Trim(mylist)
>>
>> ' in this syntax Trim is the Excel function TRIM
>> ' and not the VBA function TRIM.
>> ' The excel function TRIM should do what you aim to do
>>
>> Bye
>>
>>
>>
>> "Joe User" <joeu2004> a écrit dans le message de groupe de discussion :
>> #(E-Mail Removed)...
>>> In VBA, I want to replace multiple interstitial spaces with one space in
>>> a string variable. For example, " a b c d e f g "
>>> should become "a b c d e f g".
>>>
>>> How can I do this using just standard VBA features? (I want to avoid
>>> adding References.)
>>>
>>> The following is close to what I want:
>>>
>>> Dim mylist as String
>>> [....]
>>> mylist = Replace(Trim(mylist)," "," ")
>>>
>>> The problem is: apparently Replace starts scanning again after the
>>> replacement text. Consequently, for example, 4 spaces are reduced to 2
>>> spaces instead of 1.
>>>
>>> I 'spose I could do something like:
>>>
>>> mylist = Trim(mylist)
>>> Do
>>> mylist0 = mylist
>>> mylist = Replace(mylist," "," ")
>>> Loop until mylist = mylist0
>>>
>>> That works. But is there something better?
>>>
>>> I would like an answer to the question above, for my edification.
>>>
>>> But my purpose is to compress spaces before using Split(mylist) to
>>> separate the "words".
>>>
>>> Is there a way to do the Split without having to compress multiple
>>> spaces first?
>>>
>>> I am using Excel 2003 SP3 with VBA 6.5.1024.

>>

>

 
Reply With Quote
 
Joe User
Guest
Posts: n/a
 
      24th Dec 2009
"Charabeuh" <(E-Mail Removed)> wrote:
> I think your form is better than mine since TRIM
> is a member of the WorksheetFunction object


But Application.Trim does work. My question was: is there any difference?

To reiterate: I know they seem to behave the same. I am asking if there is
any reason to use one instead of the other. For example, performance?

Odd.... I do not see Trim listed as an Application property or method. Why
does Application.Trim (that syntax) work in the first place?


----- original message -----

"Charabeuh" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>I think your form is better than mine since TRIM is a member of the
>WorksheetFunction object
>
> The complete syntax in the object model should be:
> mylist = Application.WorksheetFunction.Trim(mylist)
>
>
>
> "Joe User" <joeu2004> a écrit dans le message de groupe de discussion :
> (E-Mail Removed)...
>> "Charabeuh" <(E-Mail Removed)> wrote:
>>> mylist = Application.Trim(mylist)

>>
>> Good. Thanks.
>>
>> What's the difference, if any, between that and:
>>
>> mylist = WorksheetFunction.Trim(mylist)
>>
>> I know they are the same functionally. My question is: is there any
>> reason to use one form instead of the other?
>>
>>
>> ----- original message -----
>>
>> "Charabeuh" <(E-Mail Removed)> wrote in message
>> news:%(E-Mail Removed)...
>>> Hello,
>>>
>>> Try this in your VBA code:
>>>
>>> mylist = Application.Trim(mylist)
>>>
>>> ' in this syntax Trim is the Excel function TRIM
>>> ' and not the VBA function TRIM.
>>> ' The excel function TRIM should do what you aim to do
>>>
>>> Bye
>>>
>>>
>>>
>>> "Joe User" <joeu2004> a écrit dans le message de groupe de discussion :
>>> #(E-Mail Removed)...
>>>> In VBA, I want to replace multiple interstitial spaces with one space
>>>> in a string variable. For example, " a b c d e f g
>>>> " should become "a b c d e f g".
>>>>
>>>> How can I do this using just standard VBA features? (I want to avoid
>>>> adding References.)
>>>>
>>>> The following is close to what I want:
>>>>
>>>> Dim mylist as String
>>>> [....]
>>>> mylist = Replace(Trim(mylist)," "," ")
>>>>
>>>> The problem is: apparently Replace starts scanning again after the
>>>> replacement text. Consequently, for example, 4 spaces are reduced to 2
>>>> spaces instead of 1.
>>>>
>>>> I 'spose I could do something like:
>>>>
>>>> mylist = Trim(mylist)
>>>> Do
>>>> mylist0 = mylist
>>>> mylist = Replace(mylist," "," ")
>>>> Loop until mylist = mylist0
>>>>
>>>> That works. But is there something better?
>>>>
>>>> I would like an answer to the question above, for my edification.
>>>>
>>>> But my purpose is to compress spaces before using Split(mylist) to
>>>> separate the "words".
>>>>
>>>> Is there a way to do the Split without having to compress multiple
>>>> spaces first?
>>>>
>>>> I am using Excel 2003 SP3 with VBA 6.5.1024.
>>>

>>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FILE COMPRESS not by ZIP or Normal windows compress Hamed Windows XP General 17 18th Jan 2007 05:00 AM
How to count interstitial blank cells? =?Utf-8?B?am9ldTIwMDRAaG90bWFpbC5jb20=?= Microsoft Excel Misc 3 16th Apr 2006 03:53 AM
What is Interstitial Ad Delivery by 180 Search Assistant Sweeneyia Windows XP Embedded 1 4th May 2004 01:55 AM
Help w/ Interstitial 180 HOUSE DETECTIVE Anti-Virus 3 30th Apr 2004 03:02 AM
Interstitial Ad Delivery Annette Windows XP General 3 9th Apr 2004 04:46 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:30 PM.