DISCOVER
×

How to Remove Brackets From a String in VBA

Updated July 20, 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.

Cite this Article A tool to create a citation to reference this article Cite this Article

About the Author

Jaime Avelar is a professional writer whose programming articles appear on various websites. He has been a software programmer since 2000. Avelar holds a Master of Science in information systems from the University of Texas at Arlington.