Extract Text Between ()

Stuck1

Board Regular
Joined
Sep 3, 2009
Messages
73
Hi all,

I don't know if anyone can quickly help with this, but is there a way of extracting just the text between two brackets ()?

I have a messy spreadsheet that is largely free text and I only need the ID's that are always between two brackets. There are several sheets to search through, so the soloution will have to search the whole workbook.

Many thanks for any help.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hi all,

I don't know if anyone can quickly help with this, but is there a way of extracting just the text between two brackets ()?

I have a messy spreadsheet that is largely free text and I only need the ID's that are always between two brackets. There are several sheets to search through, so the soloution will have to search the whole workbook.
Where do you need these ID's at?
 
Upvote 0
I just want to extrat them into a list, perhaps in a new sheet.
Give this macro a try...
Code:
Sub GetIDs()
  Dim R As Long, C As Long, List As String, ListParts() As String
  Dim vArray As Variant, WS As Worksheet
  For Each WS In Worksheets
    vArray = WS.UsedRange
    If Not IsEmpty(vArray) Then
      For R = 1 To UBound(vArray)
        For C = 1 To UBound(vArray, 2)
          If vArray(R, C) Like "*(*)*" Then List = List & Chr$(1) & Split(Replace(vArray(R, C), ")", "("), "(")(1)
        Next
      Next
    End If
  Next
  ListParts = Split(Mid(List, 2), Chr$(1))
  Worksheets.Add After:=Sheets(Sheets.Count)
  Range("A1").Resize(UBound(ListParts) + 1) = Application.Transpose(ListParts)
  Debug.Print Mid(List, 2)
End Sub
 
Upvote 0
Thanks so much for the reply and your help. It almost works, however, in some cells there is more than 1 ID. The code only seems to return one ID per cell. Also, there are multiple sheets, is there a way to run the code for the entire workbook?

Thanks again for the help.
 
Upvote 0
Wow Rick

Can you please explain how this code works especially these parts

Does the varray start from first col and row of the used range to to the last col and row?

vArray = WS.UsedRange

List = List & Chr$(1) & Split(Replace(vArray(R, C), ")", "("), "(")(1)
ListParts = Split(Mid(List, 2), Chr$(1))
Range("A1").Resize(UBound(ListParts) + 1) = Application.Transpose(ListParts)
Why is the transposed used here<why is="" the="" transpose="" used<="" html=""></why>
 
Last edited:
Upvote 0
Wow Rick

Can you please explain how this code works especially these parts

Does the varray start from first col and row of the used range to to the last col and row?

vArray = WS.UsedRange

List = List & Chr$(1) & Split(Replace(vArray(R, C), ")", "("), "(") (1)
ListParts = Split(Mid(List, 2), Chr$(1))
Range("A1").Resize(UBound(ListParts) + 1) = Application.Transpose(ListParts)
Why is the transposed used here
 
Upvote 0
Thanks so much for the reply and your help. It almost works, however, in some cells there is more than 1 ID. The code only seems to return one ID per cell. Also, there are multiple sheets, is there a way to run the code for the entire workbook?
The code I posted already does all of the worksheets in your workbook, as for the multiple ID's in individual cells... you never mentioned that was a possibility, so I did not design the code for it. This new macro should do what you now want...
Code:
Sub GetIDs()
  Dim R As Long, C As Long, X As Long, List As String, ListParts() As String
  Dim vArray As Variant, WS As Worksheet
  For Each WS In Worksheets
    vArray = WS.UsedRange
    If Not IsEmpty(vArray) Then
      For R = 1 To UBound(vArray)
        For C = 1 To UBound(vArray, 2)
          If vArray(R, C) Like "*(*)*" Then
            ListParts = Split(Replace(vArray(R, C), ")", "("), "(")
            For X = 1 To UBound(ListParts) Step 2
              List = List & Chr$(1) & ListParts(X)
            Next
          End If
        Next
      Next
    End If
  Next
  ListParts = Split(Mid(List, 2), Chr$(1))
  Worksheets.Add After:=Sheets(Sheets.Count)
  Range("A1").Resize(UBound(ListParts) + 1) = Application.Transpose(ListParts)
End Sub
 
Upvote 0
Hi Rick

Any chance you can have a look at my post and explain how the code works?

That would be appreciated
 
Upvote 0
Wow Rick

Can you please explain how this code works especially these parts

Does the varray start from first col and row of the used range to to the last col and row?

vArray = WS.UsedRange

List = List & Chr$(1) & Split(Replace(vArray(R, C), ")", "("), "(") (1)
ListParts = Split(Mid(List, 2), Chr$(1))
Range("A1").Resize(UBound(ListParts) + 1) = Application.Transpose(ListParts)
Why is the transposed used here
I'll address the parts of my code by color...

Green: I replace all closing parentheses with opening parentheses so that I wll have a common delimiter for the Split function

Purple: Split creates an array of string values using the opening parentheses as the delimiter.

Orange: The Split function always produces a zero-based array, so we can address the element of that returned arrays directly by placing the element number in parentheses (just like we do with any other array)... the text we want is located after the first delimiter, hence we want element 1 from that zero-based array.

Gray: This just extends the text in the List variable by concatentating the text retrieved by the Split function onto the existing text in the List variable with a character the user cannot type at the keyboard (I use the characteGray: This just extends the text in the List variable by concatentating the text retrieved by the Split function onto the existing text in the List variable with a character the user cannot type at the keyboard (I use the character whose ASCII value is 1, but any character guaranteed not to be in the text concatenated onto List would do).


Blue: Because the first thing concatenated onto List is Chr$(1), it will start the List when List is first empty, so we want to remove it... we do that by retrieving all characters starting with the second character... remember, the first character will always be Chr$(1).


Red: We create an array of names by splitting List using Chr$(1) as the delimiter.


Brown: A one-dimensional array (what Split produces) can be assigned directly to a horizontal range of cells, but since we want a vertical list, we must transpose the values in the array.r
 
Upvote 0

Forum statistics

Threads
1,214,627
Messages
6,120,610
Members
448,973
Latest member
ChristineC

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