How to Extract Text between Brackets

,

We talked that how to extract data between parentheses in the previous post, and this post will guide you how to create similar formula to extract text between brackets in a cell. This post will also show you how to write an User Defined function with VBA code to extract text between brackets in Excel.

1. Extract text between brackets Using a Formula

If you want to extract text between brackets in a cell, you need to create a formula based on the SEARCH function and the MID function.

The MID function will extract a specific number of characters at a specific starting position. You need to know how to get the starting position of text between brackets in a test string, you can use the SEARCH function to get the position of the first left/right bracket characters.

Next, you can get the starting position of the text that you want to extract and the length of the text. So we can write down the following excel formula:

=MID(B1, SEARCH("[", B1)+1, SEARCH("]",B1) - SEARCH("[",B1)-1)
excel text between brackets1

For more detailed information about this formula, you can continue to read the below post:  how to extract text between parentheses in excel.

2. Extract Text between Brackets Using a User-Defined Function (VBA)

You can also use a user-defined function in VBA to extract text between brackets in Microsoft Excel 2013/2016/2019. Just do the following steps:

Step1: open the Visual Basic for Applications (VBA) Editor by pressing ALT + F11 or navigating to the Developer tab and clicking on Visual Basic command.

Step2: In the VBA Editor, click on Insert in the menu and then select Module to add a new module where you can write the VBA code.

Step3: In the module window, write the below VBA code for the user-defined function to extract text between brackets.

How to Extract Text between Brackets10.png
Function ExtractTextBetweenBrackets(ByVal text As String) As String
    Dim startPos As Integer
    Dim endPos As Integer
    
    startPos = InStr(text, "(")
    endPos = InStr(text, ")")
    
    If startPos > 0 And endPos > 0 Then
        ExtractTextBetweenBrackets = Mid(text, startPos + 1, endPos - startPos - 1)
    Else
        ExtractTextBetweenBrackets = ""
    End If
End Function

Step4: type the below User Defined Function to extract the text between brackets. And then press Enter key to apply it.

=ExtractTextBetweenBrackets(B1)

Step5: The cell will now display the text “text” which is the content between the first set of brackets.

How to Extract Text between Brackets12.png

3. Video: Extract Text between Brackets

This tutorial video will explore two efficient methods to extract text between brackets in Excel.

4. SAMPLE FIlES

Below are sample files in Microsoft Excel that you can download for reference if you wish.

5. Related Formulas

  • Extract Text between Parentheses
    If you want to extract text between parentheses in a cell, then you can use the search function within the MID function to create a new excel formula…
  • Check If Cell Contains All Values from Range
    If you want to check if a cell contains all values in a list, you can use a combination of the SEARCH function, the SUMPRODUCT function, the ISNUMBER function and COUNTA function…

6. Related Functions

  • Excel SEARCH function
    The Excel SEARCH function returns the number of the starting location of a substring in a text string.The syntax of the SEARCH function is as below:= SEARCH  (find_text, within_text,[start_num])…
  • Excel MID function
    The Excel MID function returns a substring from a text string at the position that you specify.The syntax of the MID function is as below:= MID (text, start_num, num_chars)…

Leave a Reply