How to Remove Brackets From a String in VBA

Written by jaime avelar | 13/05/2017

As a computer programmer, you may need to replace characters in a string variable in your VBA applications. In VBA, you can use the "Replace" function to replace a character or set of characters rather than writing code to loop through every character in your string. The "Replace" function returns a string in which it replaces a specified substring with another substring.

Launch Microsoft Office Excel, click the "Developer" tab and click "Visual Basic."

Click the "Insert" field and click "Module" to insert a new module.

Type the following to create a new sub procedure:

Private Sub remove Brackets()

Add the following code to create a string variable to hold a string with brackets:

Dim tempStr As String

tempStr = "[brackets have been removed]"

Type the following code to remove the brackets from the string:

tempStr = Replace(tempStr, "[", "")

tempStr = Replace(tempStr, "]", "")

Add the following line to display the string using a message box:

MsgBox (tempStr)

Type "End Sub" to end the procedure.

Press "F5" to run.

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.