View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Ron Rosenfeld Ron Rosenfeld is offline
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