Replace Commas Outside Parentheses with Spaces in Microsoft Office VBA
Question details
Replace commas outside parenthesized expressions with spaces while preserving commas inside them.

- Product
- Microsoft Office VBA
- Device & OS
- not provided
- Scenario
- A VBA routine needs to process text such as logn(x,2),1,100 without changing the comma in the function arguments.
- Observed behavior
- The desired output is logn(x,2) 1 100, with only commas outside parentheses replaced.
Make sure you can open the Visual Basic Editor in your Office application and have a sample string ready to test. The function below assumes the input has balanced parentheses.
Track Parenthesis Depth in a VBA Function
Use this function to replace outside commas while preserving commas in both ordinary and nested parentheses.
The function reads the input one character at a time. A depth counter increases at each opening parenthesis and decreases at each closing parenthesis; a comma is replaced only when the depth is zero.
In an Office application that supports VBA, open the Visual Basic Editor. For example, in Excel, select Developer > Visual Basic.
In the editor, select Insert > Module and paste the following code: Function ReplaceOutsideCommas(ByVal inputText As String) As String Dim i As Long Dim depth As Long Dim ch As String Dim result As String For i = 1 To Len(inputText) ch = Mid$(inputText, i, 1) Select Case ch Case "(" depth = depth + 1 Case ")" depth = depth - 1 Case "," If depth = 0 Then ch = " " End Select result = result & ch Next i ReplaceOutsideCommas = result End Function
In the VBA editor, select View > Immediate Window, enter ? ReplaceOutsideCommas("logn(x,2),1,100"), and press Enter. The result should be logn(x,2) 1 100.
In your existing VBA routine, call ReplaceOutsideCommas with the string to process, for example: result = ReplaceOutsideCommas(inputText).

Work with Office Files in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for opening and editing Office files. Its format compatibility helps you move everyday documents and spreadsheets between applications. VBA support varies by platform and edition, so check compatibility before relying on an existing macro.

Frequently Asked Questions
Why not use VBA's Replace function on every comma?
Replace(inputText, ",", " ") changes every comma, including the comma in logn(x,2). Tracking parenthesis depth lets the function replace only commas outside parentheses.
Does the function handle nested parentheses?
Yes. Each opening parenthesis increases the depth counter, and each closing parenthesis decreases it. Commas are replaced only when the counter is zero.
Where can I test the function in Excel?
Open Developer > Visual Basic, insert the function into a module, and select View > Immediate Window. Enter ? ReplaceOutsideCommas("logn(x,2),1,100") and press Enter.
Will the function ignore parentheses inside quoted text?
No. It counts every opening and closing parenthesis. If quoted text can contain parentheses that should be ignored, add logic to track whether the current character is inside a quoted string.




