Extract text outside multiple parenthesis

pete1229

New Member
Joined
Nov 6, 2020
Messages
21
Office Version
  1. 365
Platform
  1. Windows
I want to extract all text outside of any parenthesis. I can get the text from the first occurence of a parenthesis using:

=TRIM(LEFT(A2,FIND(" (",A2&" (")))

But now sure how to do this for multiple occurences. Below is an example.

TextExpected result
Cucumber (1)Cucumber
Cucumber (1) Tomato (2)Cucumber Tomato
Cucumber (1) Tomato (2) Lettuce (3)Cucumber Tomato Lettuce
 
As a double check what does this return
Excel Formula:
=TEXTJOIN(" ",,FILTERXML("<k><m>"&SUBSTITUTE(A4," ","</m><m>")&"</m></k>","//m[3]"))
Also would you be happy with a UDF (macro) rather than a formula?
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
As a double check what does this return
Excel Formula:
=TEXTJOIN(" ",,FILTERXML("<k><m>"&SUBSTITUTE(A4," ","</m><m>")&"</m></k>","//m[3]"))
Also would you be happy with a UDF (macro) rather than a formula?
The formula returns "Tomato".
I would be very happy with a macro if that makes it easier, thank you.
 
Upvote 0
Ok, how about
VBA Code:
Function Pete(Txt As String) As String
   Dim Ary As Variant
   Dim i As Long
   
   Ary = Split(Txt)
   For i = 0 To UBound(Ary)
      If InStr(1, Ary(i), "(") = 0 Then Pete = Pete & " " & Ary(i)
   Next i
   Pete = Trim(Pete)
End Function
used like
+Fluff 1.xlsm
AB
1TextExpected result
2Cucumber (1)Cucumber
3Cucumber (1) Tomato (2)Cucumber Tomato
4Cucumber (1) Tomato (2) Lettuce (3)Cucumber Tomato Lettuce
5Cucumber (1) Tomato Puree (2) Lettuce (3)Cucumber Tomato Puree Lettuce
Master
Cell Formulas
RangeFormula
B2:B5B2=pete(A2)
 
Upvote 0
Solution
If you don't mind fixing your data in place, you can use this non-looping macro to do it...
VBA Code:
Sub pete1229()
  Application.ScreenUpdating = False
  With Range("A1", Cells(Rows.Count, "A").End(xlUp))
    .Replace "(*)", "", xlPart, , , , False, False
    .Value = Evaluate("TRIM(" & .Address & ")")
  End With
  Application.ScreenUpdating = True
End Sub
If you need your fixed data in another column, then use this instead...
VBA Code:
Sub pete1229()
  Application.ScreenUpdating = False
  With Range("A1", Cells(Rows.Count, "A").End(xlUp))
    .Copy .Cells(1).Offset(, 1)
    With .Offset(, 1)
      .Replace "(*)", "", xlPart, , , , False, False
      .Value = Evaluate("TRIM(" & .Address & ")")
    End With
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,259
Members
452,626
Latest member
huntinghunter

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top