ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Removing extra spaces in a string (https://www.excelbanter.com/excel-programming/423410-removing-extra-spaces-string.html)

John

Removing extra spaces in a string
 
What's a quick way to remove multiple spaces between words in a string?

I tried ProjectName = Replace(ProjectName, " ", " "), but this sometimes
leaves multiple spaces between words in ProjectName.

I appreciate your help, -John

Gord Dibben

Removing extra spaces in a string
 
Application.Trim(string) to remove extra spaces


Gord Dibben MS Excel MVP

On Wed, 4 Feb 2009 11:41:59 -0800, John
wrote:

What's a quick way to remove multiple spaces between words in a string?

I tried ProjectName = Replace(ProjectName, " ", " "), but this sometimes
leaves multiple spaces between words in ProjectName.

I appreciate your help, -John



joel

Removing extra spaces in a string
 
if you have 3 spaces your code will only remove one of the spaces still
leaving two try this

'Test for 2 blanks in a row
Do while Instr(ProjectName, " ") 0 then
ProjectName = Replace(ProjectName, " ", " ")
Loop


"John" wrote:

What's a quick way to remove multiple spaces between words in a string?

I tried ProjectName = Replace(ProjectName, " ", " "), but this sometimes
leaves multiple spaces between words in ProjectName.

I appreciate your help, -John


Rick Rothstein

Removing extra spaces in a string
 
No "Then" keyword required in a Do statement. Also, while some may not
consider it as clear, you don't have to test the InStr function's return
value for being greater than 0... VB will consider any non-zero value for a
logical expression as True.

Do While InStr(ProjectName, " ")
ProjectName = Replace(ProjectName, " ", " ")
Loop

--
Rick (MVP - Excel)


"Joel" wrote in message
...
if you have 3 spaces your code will only remove one of the spaces still
leaving two try this

'Test for 2 blanks in a row
Do while Instr(ProjectName, " ") 0 then
ProjectName = Replace(ProjectName, " ", " ")
Loop


"John" wrote:

What's a quick way to remove multiple spaces between words in a string?

I tried ProjectName = Replace(ProjectName, " ", " "), but this sometimes
leaves multiple spaces between words in ProjectName.

I appreciate your help, -John




All times are GMT +1. The time now is 08:40 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com