View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.misc
Ron Rosenfeld Ron Rosenfeld is offline
external usenet poster
 
Posts: 5,651
Default Extract text on right of various length from cells

On Wed, 01 Jul 2009 21:54:24 -0400, Ron Rosenfeld
wrote:

On Wed, 1 Jul 2009 11:22:01 -0700, HammerD
wrote:

Sorry - my mistake... the cells have the text enclosed in parentheses. And I
want just the text - not the number or parentheses. There may be 3 or more
words within the parentheses.
(ex: i.e. - 260 (SMALL THINGS), 261 (THINGS)


I'm guessing that you want to retain the commas.

Here is a User Defined Function that will remove all digits, parentheses, and
extraneous spaces from your string; leaving you with the text, and the
separating commas.

To enter this User Defined Function (UDF), <alt-F11 opens the Visual Basic
Editor.
Ensure your project is highlighted in the Project Explorer window.
Then, from the top menu, select Insert/Module and
paste the code below into the window that opens.

To use this User Defined Function (UDF), enter a formula like

=RemDigPar(A1)

(where A1 contains your string to process) in some cell.

=====================================
Option Explicit
Function RemDigPar(s As String) As String
Dim re As Object
Const sPat As String = "[\d()]+"

Set re = CreateObject("vbscript.regexp")
re.Pattern = sPat
re.Global = True

RemDigPar = Application.WorksheetFunction.Trim _
(re.Replace(s, ""))
End Function
==============================
--ron


Given Rick's (valid) concern about extraneous spaces that might not be taken
care of by the worksheetfunction.trim, I have change the regex pattern to take
care of that, and to also eliminate having to use worksheetfunction.trim at
all:

================================
Option Explicit
Function RemDigPar(s As String) As String
Dim re As Object
Const sPat As String = "[\d()]+|(^\s+)|(\s+$)|(\s(?=\W))"

Set re = CreateObject("vbscript.regexp")
re.Pattern = sPat
re.Global = True

RemDigPar = re.Replace(s, "")
End Function
===============================
--ron