Skip to main content

Posts

Showing posts with the label Search a String for Words in VBA

Search a String for Words in VBA

Using VBA, How can I search a string for words surround by ( ) and remove them from the string. There may be certain cases where I have a string equal to something like: "dog, cat, (pig), sheep, (elephant)". I need help creating a code that will search the string and remove the words surrounded in brackets resulting in: "dog, cat, sheep". To make this a bit more challenging, in addition to removing words surrounded by ( ) I would like the comma's to adjust as well so I don't end up with: "dog. cat. . sheep, ," This would mean if sheep became the last word in the string it would no longer be followed by a comma. This is a code suggestion for the same 1. Function FilterBetween(Data As String, _ Optional Delimiter As String = ",", _ Optional OnLeft As String = "(", _ Optional OnRight As String = ")") _ As String 'remove anything in a delimited string which is surrounded by ' a le