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.
- Skill level:
- Moderately Easy
Other People Are Reading
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:
Type "End Sub" to end the procedure.
Press "F5" to run.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for