Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default Replace Underscores Between Words from an Object with a Single Spa

I have an object that is filled with many underscores between texts. I
recently got help from your forum on how to remove leading underscores which
works great and now I need help in replacing a line of underscores between
words with a single space.

Thank you again for your help!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Replace Underscores Between Words from an Object with a Single Spa

What sort of "object" are you referring to? If the text of the
"object" is in a variable named S, you can use code like the
following. The first Replace removes all the underscore characters.
The loop single spaces the remaining text (converting 2 spaces to 1
space):

Dim S As String
Dim N As Long
S = "your string here"
S = Replace(S, "-", vbNullString)
N = InStr(1, S, Space(2))
Do Until N = 0
S = Replace(S, Space(2), Space(1))
N = InStr(1, S, Space(2))
Loop
Debug.Print S

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)



On Tue, 30 Dec 2008 13:37:34 -0800, TomP
wrote:

I have an object that is filled with many underscores between texts. I
recently got help from your forum on how to remove leading underscores which
works great and now I need help in replacing a line of underscores between
words with a single space.

Thank you again for your help!

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Replace Underscores Between Words from an Object with a Single Spa

I'm thinking this might be more what you are looking for. When I ran Chip's
code, I got a string without spaces at all.

Sub merge()
Dim S As String
S = "Your_String"
S = Replace(S, "_", Space(1))
MsgBox S
End Sub

"TomP" wrote:

I have an object that is filled with many underscores between texts. I
recently got help from your forum on how to remove leading underscores which
works great and now I need help in replacing a line of underscores between
words with a single space.

Thank you again for your help!

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Replace Underscores Between Words from an Object with a Single Spa

On Tue, 30 Dec 2008 13:37:34 -0800, TomP
wrote:

I have an object that is filled with many underscores between texts. I
recently got help from your forum on how to remove leading underscores which
works great and now I need help in replacing a line of underscores between
words with a single space.

Thank you again for your help!


============================
Option Explicit
Function ReplaceUnderscore(str As String) As String
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = "\s*_+\s*"
ReplaceUnderscore = re.Replace(str, " ")
End Function
==========================

This routine will also replace any spaces that might be before or after the
underscore. so:

Now is________the time -- Now is the time

Now is ____________ the time -- Now is the time

If that is not desired behavior, then change
re.Pattern = "_+"

--ron
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Replace Underscores Between Words from an Object with a Single Spa

If your text is in a variable named S, then this single line of code will
remove leading and trailing underscores and reduce all internal underscores
to a single space...

S_without_underscores = WorksheetFunction.Trim(Replace(S, "_", " "))

The above statement assumes either that there are no internal spaces or that
if there are, they will be combined with the spaces produced by converting
the underscores to spaces and then reduced to a single space afterwards. I
am pretty sure that it is what you are after. However, if you must preserve
existing multiple internal spaces, then this single line of code may do what
you want...

S_without_underscores = Replace(WorksheetFunction.Trim(Replace(Replace( _
S, " ", Chr(1)), "_", " ")), Chr(1), " ")

--
Rick (MVP - Excel)


"TomP" wrote in message
...
I have an object that is filled with many underscores between texts. I
recently got help from your forum on how to remove leading underscores
which
works great and now I need help in replacing a line of underscores between
words with a single space.

Thank you again for your help!




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Replace Underscores Between Words from an Object with a Single Spa

On Wed, 31 Dec 2008 01:19:35 -0500, "Rick Rothstein"
wrote:

S_without_underscores = WorksheetFunction.Trim(Replace(S, "_", " "))


Nice and simple
--ron
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Replace Underscores Between Words from an Object with a Single Spa

Thanks... the worksheet TRIM function has a definite advantage over VB's
Trim function for this particular question.

--
Rick (MVP - Excel)


"Ron Rosenfeld" wrote in message
...
On Wed, 31 Dec 2008 01:19:35 -0500, "Rick Rothstein"
wrote:

S_without_underscores = WorksheetFunction.Trim(Replace(S, "_", " "))


Nice and simple
--ron


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Replace Underscores Between Words from an Object with a Single Spa

On Wed, 31 Dec 2008 11:43:08 -0500, "Rick Rothstein"
wrote:

Thanks... the worksheet TRIM function has a definite advantage over VB's
Trim function for this particular question.


Assuming, as you pointed out, that the OP wants all <multiple spaces in the
document to be condensed to a single <space.
--ron
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default Replace Underscores Between Words from an Object with a Single

Thank you for your help! The Function worked perfectly! Happy New Year.

Tom

"Ron Rosenfeld" wrote:

On Tue, 30 Dec 2008 13:37:34 -0800, TomP
wrote:

I have an object that is filled with many underscores between texts. I
recently got help from your forum on how to remove leading underscores which
works great and now I need help in replacing a line of underscores between
words with a single space.

Thank you again for your help!


============================
Option Explicit
Function ReplaceUnderscore(str As String) As String
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = "\s*_+\s*"
ReplaceUnderscore = re.Replace(str, " ")
End Function
==========================

This routine will also replace any spaces that might be before or after the
underscore. so:

Now is________the time -- Now is the time

Now is ____________ the time -- Now is the time

If that is not desired behavior, then change
re.Pattern = "_+"

--ron

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
How to Strip Leading Spaces and Underscores from Object TomP Excel Programming 6 December 30th 08 02:12 PM
Replace several words to One Wanna Learn Excel Discussion (Misc queries) 2 April 30th 08 01:56 PM
How to Replace multiple words to replace using excell ramsun Excel Programming 1 August 10th 06 01:52 PM
Separating words in a single cell ibere Excel Discussion (Misc queries) 11 June 15th 06 10:14 PM
How can I replace spaces in text cells (excel) with underscores? JB2006 New Users to Excel 2 April 20th 06 06:05 PM


All times are GMT +1. The time now is 02:49 AM.

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"