+ Reply to Thread
Results 1 to 9 of 9

Delete everything between brackets , even when nested

  1. #1
    Registered User
    Join Date
    02-21-2024
    Location
    Middx UK
    MS-Off Ver
    2003
    Posts
    41

    Delete everything between brackets , even when nested

    I have a small macro which deletes everything between brackets

    Please Login or Register  to view this content.
    This works fine , but fails where the brackets are nested.

    So for example

    Salsoul Orchestra - Runaway (CD, Maxi) (Club Classics (2) - CLCL 012)

    becomes

    Salsoul Orchestra - Runaway - CLCL 012).

    when it should be

    Salsoul Orchestra - Runaway

    Can someone suggest some VBA which will cope with nested brackets and delete everything within them?

    Grateful for any advice.


    Last edited by CaMeSuffit; 08-25-2024 at 05:08 PM.
    Currently using Access , Word and Excel 2003

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2407
    Posts
    25,831

    Re: Delete everything between brackets , even when nested

    Try this pattern but repeat your find & replace at least twice.

    \([!\(]@\)

    This will delete nested brackets from inside out but requires a second pass to get the outer ones. I added a third one in off chance that you have nesting to three levels.

    Please Login or Register  to view this content.
    If this does not work please provide a sample Word file for testing.

    Another more complicated but more powerful method is to use regexp, which is greedy. Word pattern matching is lazy.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    02-21-2024
    Location
    Middx UK
    MS-Off Ver
    2003
    Posts
    41

    Re: Delete everything between brackets , even when nested

    Thanks 6StringJazzer - this works perfectly and solves the issue. Problem solved!


  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2407
    Posts
    25,831

    Re: Delete everything between brackets , even when nested

    You're welcome and thanks for the rep!

  5. #5
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,753

    Re: Delete everything between brackets , even when nested

    Better:
    Please Login or Register  to view this content.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2407
    Posts
    25,831

    Re: Delete everything between brackets , even when nested

    Yes, that's better, I was unaware of the Found property.

  7. #7
    Registered User
    Join Date
    09-12-2024
    Location
    alaska
    MS-Off Ver
    19
    Posts
    1

    Re: Delete everything between brackets , even when nested

    When dealing with text that contains nested brackets, like "Salsoul Orchestra - Runaway (CD, Maxi) (Club Classics (2) - CLCL 012)," it's crucial to have a VBA script that can handle such complexity. The provided VBA solution needs to accurately remove all content enclosed in brackets, including those nested within each other. A reliable approach involves using a recursive function that processes each level of nested brackets until all are removed. If you're looking to improve your VBA script for this purpose, ensure it can handle multiple layers of nesting to achieve a clean, bracket-free result. For more detailed guidance, check out specialized VBA forums or tutorials.

  8. #8
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,753

    Re: Delete everything between brackets , even when nested

    Your point being? Did you actually examine & test the solutions posted?

  9. #9
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2407
    Posts
    25,831

    Re: Delete everything between brackets , even when nested

    Quote Originally Posted by ribiwo1345 View Post
    When dealing with text that contains nested brackets, like "Salsoul Orchestra - Runaway (CD, Maxi) (Club Classics (2) - CLCL 012)," it's crucial to have a VBA script that can handle such complexity. The provided VBA solution needs to accurately remove all content enclosed in brackets, including those nested within each other. A reliable approach involves using a recursive function that processes each level of nested brackets until all are removed. If you're looking to improve your VBA script for this purpose, ensure it can handle multiple layers of nesting to achieve a clean, bracket-free result. For more detailed guidance, check out specialized VBA forums or tutorials.
    This looks like an AI response. Not a good look for your first post. Plus the thread is already marked SOLVED. Plus the post contributes nothing to a solution.

    If this continues you are subject to infractions up to banning.
    Last edited by 6StringJazzer; 09-12-2024 at 10:02 AM.

+ 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. Replies: 8
    Last Post: 02-02-2023, 11:28 AM
  2. Formula to remove Brackets and Text in brackets
    By jshaw82 in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 06-27-2022, 09:41 AM
  3. [SOLVED] Delete brackets and their content when including determined word X
    By edoard0 in forum Excel General
    Replies: 4
    Last Post: 11-12-2018, 04:04 PM
  4. Delete digits within brackets
    By ambharat in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 05-15-2013, 04:09 AM
  5. [SOLVED] How to delete whatever is in the cell that is in brackets?
    By thingstodo123 in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 11-04-2012, 08:02 PM
  6. Replies: 5
    Last Post: 04-19-2012, 09:02 AM
  7. macro Delete string text between brackets in several cells
    By raparigo74 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-27-2011, 04:19 AM

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