Wookiee
Active Member
- Joined
- Nov 27, 2012
- Messages
- 440
- Office Version
- 365
- 2019
- Platform
- Windows
Hello, MrExcel Community! I have a question which I have so far been unable to answer by googling. I hope you can help.
I have VBA code which is supposed to clean up some undesirable elements from text strings. In some cases, there will be a notation between brackets in the text strings and I would like to remove the entire bracketed segment of the string, but I have been unable to do so.
To illustrate, a couple of examples would be:
"Darth Vader [Empire]" becomes simply "Darth Vader"
"Han Solo [Rebellion]" converts to "Han Solo"
I even tried recording a macro where I used a wildcard in my Search/Replace. Although the recorded code doesn't seem significantly different from what I had already written, my code doesn't strip off the bracketed text.
Recorded Macro
My Code
I'm assuming that the difference in the functionality might have to do with the fact that the recorded macro worked on cells within a worksheet, whereas my code is attempting to manipulate a string which exists solely in the VBA environment. Can anyone point out what I'm missing or provide an alternate method?
Thanks, all!
I have VBA code which is supposed to clean up some undesirable elements from text strings. In some cases, there will be a notation between brackets in the text strings and I would like to remove the entire bracketed segment of the string, but I have been unable to do so.
To illustrate, a couple of examples would be:
"Darth Vader [Empire]" becomes simply "Darth Vader"
"Han Solo [Rebellion]" converts to "Han Solo"
I even tried recording a macro where I used a wildcard in my Search/Replace. Although the recorded code doesn't seem significantly different from what I had already written, my code doesn't strip off the bracketed text.
Recorded Macro
Code:
Selection.Replace What:="
[*]", Replacement:=""
My Code
Code:
strSource = Replace(strSource, "
[*]", "")
I'm assuming that the difference in the functionality might have to do with the fact that the recorded macro worked on cells within a worksheet, whereas my code is attempting to manipulate a string which exists solely in the VBA environment. Can anyone point out what I'm missing or provide an alternate method?
Thanks, all!