Regex with Replace in an Excel cell - excluding string in brackets

Peach777 1 Reputation point
2021-12-05T17:46:18.27+00:00

Hello, ich have a question regards replacing commas in Excel 365 cell with ### except in brackets.

With Regex? But how?

Bsp.:
Hello (bla, bla, blubb), This is a test, but hopefully you have a solution (this would be great, awesome and super), I hope to get an answer :-)

After replacing it should look like: Replace „,“ with „###“, BUT not in the brackets (…)

Hello (bla, bla, blubb)### This is a test### but hopefully you have a solution (this would be great, awesome and super)### I hope to get an answer :-)

Any idea?

Thank you!

Greetings peach777

Microsoft 365 and Office | Excel | For business | Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Emily Hua-MSFT 27,796 Reputation points
    2021-12-06T05:19:14.443+00:00

    Hi @Peach777

    Welcome to Q&A forum ~

    Please check whether the following formual is helpful.
    =SUBSTITUTE(LEFT(A1,FIND("(",A1)-1),",","###")&MID(A1,FIND("(",A1),FIND(")",A1)-FIND("(",A1)+1)&SUBSTITUTE(RIGHT(A1,LEN(A1)-FIND(")",A1)),",","###")
    155127-capture8.jpg

    • LEFT(A1,FIND("(",A1)-1) is to get the content on the left of "(".
      SUBSTITUTE(LEFT(A1,FIND("(",A1)-1)),",","###") is to replace "," with "###" for the result above.
    • MID(A1,FIND("(",A1),FIND(")",A1)-FIND("(",A1)+1) is to the brackets and the content between them.
    • RIGHT(A1,LEN(A1)-FIND(")",A1))" is to get the content on the right of ")".
      SUBSTITUTE(RIGHT(A1,LEN(A1)-FIND(")",A1)),",","###") is to replace "," with "###" for the result above.

    -------- Update ---------

    First of all, I found a piece of code about custom function from lecxe's sharing on the thread "Remove commas if they are between brackets". (Please Note: Since the web site is not hosted by Microsoft, the link may change without notice. Microsoft does not guarantee the accuracy of this information.)

    Then I change the "" in "!" as following to replace all commas in all brackets with exclamation mark. If needed, you could use other symbols you like.

    Function DelCommaBrk(s As String) As String  
      
    With CreateObject("VBScript.RegExp")  
        .Pattern = ",(?=[^(]*\))"  
        .Global = True  
        DelCommaBrk = .Replace(s, "!")  
    End With  
    End Function  
    

    You could press Alt + F11 to open VBA Editor, insert a new module, then put this code into this module.
    Go back to Excel worksheet, use this custom function DelCommaBrk to change all commas in all brackets to "!".
    156006-capture31.jpg
    And then use the "SUBSTITUTE" function to replace all commas to "###".
    156114-capture32.jpg
    At last, you could still use "SUBSTITUTE" function to replace all "!" in brackets to "," back.
    156131-capture33.jpg

    Hope the information is helpful.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.