Macro to extract string withing the parentheses

zabiullakhan

Active Member
Joined
Aug 30, 2010
Messages
310
I have range of cells which have the data in the below format

Code:
Bangalore (India); Manila (Philippines)
Bangalore (India);
USA
USA; India

When i run the macro on the above string need to extract the text within the parentheses
OutPut as below

Code:
India; Philippines
India;
USA 
USA; India

Please help :banghead:
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Are the first and the fourth rows of your data one column or two? Meaning can there be more than one set of parenthesis in a cell?
 
Upvote 0
How about a UDF (user defined function)? Put this code is the same module that you would put a macro in...
Code:
Function InParens(S As String) As String
  Dim X As Long, Parts() As String
  If Len(S) = 0 Then Exit Function
  If InStr(S, ")") Then
    Parts = Split(S, ")")
    For X = 0 To UBound(Parts) - 1
      InParens = InParens & "; " & Split(Parts(X), "(")(1)
    Next
    InParens = Mid(InParens, 3)
  Else
    InParens = S
  End If
End Function
Now, go back to your worksheet. Assuming text is in the range A1:A4, put this formula in B1 and copy it down to B4 (or beyond if you want it in placed for future entries to Column A)...

=InParens(A1)
 
Upvote 0
Code:
Function RemPar(Str As String) As String

    Dim re As Object
    
    Application.Volatile

    Set re = CreateObject("VBScript.RegExp")

    With re
        .Global = True
        .Pattern = "\s+\(\w+\)"
    End With
    
    RemPar = re.Replace(Str, vbNullString)

End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,595
Members
452,927
Latest member
whitfieldcraig

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