View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein Rick Rothstein is offline
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!