+ Reply to Thread
Results 1 to 11 of 11

Extracting all sets of brackets

  1. #1
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Extracting all sets of brackets

    Hi,

    I have a variable string that could be like this

    myStr = ((ab OR dc) AND (ef OR G OR mg) AND (aF OR abd OR what ever))

    The output I want is
    ab OR dc
    ef OR G OR mg
    aF OR abd OR what ever

    Help would be greatly appreciated

    Best Regards
    Imran Bhatti
    Teach me Excel VBA

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Extracting all sets of brackets

    Please Login or Register  to view this content.

  3. #3
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,416

    Re: Extracting all sets of brackets

    Here is a function that can be used within your own VBA code or as a UDF (user defined function) in a worksheet formula...
    Please Login or Register  to view this content.
    Examples:

    Within VBA Code
    --------------------------------
    MyStr = "((ab OR dc) AND (ef OR G OR mg) AND (aF OR abd OR what ever))"
    MsgBox Brackets(MyStr)


    As a UDF in a cell
    --------------------------------
    =Brackets(A1)

    NOTE: If you use is as a UDF, don't forget to turn Wrap Text on for the cell you put the formula in.
    Last edited by Rick Rothstein; 10-23-2019 at 05:40 AM.

  4. #4
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: Extracting all sets of brackets

    @jindon. it worked as expected. Hat's off to you

  5. #5
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: Extracting all sets of brackets

    Quote Originally Posted by Rick Rothstein View Post
    Here is a function that can be used within your own VBA code or as a UDF (user defined function) in a worksheet formula...
    Please Login or Register  to view this content.
    Examples:

    Within VBA Code
    --------------------------------
    MyStr = "((ab OR dc) AND (ef OR G OR mg) AND (aF OR abd OR what ever))"
    MsgBox Brackets(MyStr)


    As a UDF in a cell
    --------------------------------
    =Brackets(A1)

    NOTE: If you use is as a UDF, don't forget to turn Wrap Text on for the cell you put the formula in.
    Your solution produced an error like the attached.
    Attached Images Attached Images

  6. #6
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,915

    Re: Extracting all sets of brackets

    You need to add:

    Please Login or Register  to view this content.
    or use:

    Please Login or Register  to view this content.
    for the parameter.
    Rory

  7. #7
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: Extracting all sets of brackets

    Thanks rory for the correction. it is working and now I have 2 solutions.

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Extracting all sets of brackets

    JFYI,

    Works differently when string like

    e.g.
    "(((a and b) + (c or d) *(e or f) and (g or h))) or something else"

    Mine is only extract in between opening bracket and closing bracket and ignore other.

  9. #9
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: Extracting all sets of brackets

    Thanks jindon, chances are more than 2 brackets in the beginning and ending but no chance of non alpha characters in my situation. I will post a new thread if I come across such situation during my project.

    Thanks once again for pointing out.

  10. #10
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,416

    Re: Extracting all sets of brackets

    Quote Originally Posted by ImranBhatti View Post
    Your solution produced an error like the attached.
    I always declare my variables as to their data type (myStr As String for this one), but you left it as a Variant (the default data type) which is why the error occurred. Thanks to rorya for providing the correction. I usually remember to use the ByVal when posting in forums, but for some reason I forgot this time.
    Last edited by Rick Rothstein; 10-23-2019 at 10:27 AM.

  11. #11
    Registered User
    Join Date
    10-11-2019
    Location
    San Jose, CA
    MS-Off Ver
    2016
    Posts
    2

    Re: Extracting all sets of brackets

    Brilliant - I had a very similar issue and this really helped!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Extracting data within brackets
    By paradise2sr in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-09-2017, 06:27 AM
  2. [SOLVED] Extracting content from within brackets
    By L.LEE in forum Excel General
    Replies: 1
    Last Post: 04-06-2017, 09:34 PM
  3. Extracting Text Between Brackets
    By McNulty in forum Excel General
    Replies: 6
    Last Post: 05-17-2016, 07:16 PM
  4. [SOLVED] Help with extracting dates out of brackets: complication...
    By leopoldc in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 10-31-2013, 11:14 PM
  5. Extracting numerical data from brackets
    By gapink in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 11-10-2012, 11:15 AM
  6. Excel 2007 : Extracting text from within brackets
    By EMarky in forum Excel General
    Replies: 5
    Last Post: 08-09-2010, 03:50 PM
  7. Extracting a value from between brackets
    By James in forum Excel General
    Replies: 3
    Last Post: 02-04-2005, 11:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1