VBA & Macro Problems

Replace Commas Outside Parentheses with Spaces in Microsoft Office VBA

Phi Hung VoPhi Hung Vo Nov 20, 2026 869 views

Question details

Replace commas outside parenthesized expressions with spaces while preserving commas inside them.

Replace Commas Outside Parentheses with Spaces in Microsoft Office VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

In an Office application that supports VBA, open the Visual Basic Editor. For example, in Excel, select Developer > Visual Basic.

2
Add the function

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

3
Test the result

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.

4
Call the function

In your existing VBA routine, call ReplaceOutsideCommas with the string to process, for example: result = ReplaceOutsideCommas(inputText).

Track Parenthesis Depth in a VBA Function
Input limitations: The function treats every parenthesis as a delimiter, including parentheses inside quoted text. It does not validate unbalanced parentheses.
Free Microsoft Office alternative

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.

Open and edit spreadsheets containing the text you need to processCompatible with Microsoft Office formats such as XLSX, DOCX, and PPTXFamiliar interface for an easier transition from Microsoft OfficeFree, lightweight tools for everyday document work
microsoft office alternative - wps office

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.