Forum Discussion
inarbeth
Jan 31, 2024Copper Contributor
Macro to remove brackets
I wonder if any VBA experts can help. I found online the following macro to remove square brackets. Sub RemoveSquareBrackets() Dim oRng As Range Set oRng = ActiveDocument.Range With oRng.Find ...
inarbeth Use
Dim oRng As Range
Set oRng = Selection.Range
With oRng
.Start = ActiveDocument.Range.Start
.Start = .Start + InStrRev(.Text, "(") - 1
.End = ActiveDocument.Range.End
.End = .Start + InStr(.Text, ")")
.Characters(1).Delete
.Characters(.Characters.Count).Delete
.Select
End With
inarbeth
Feb 01, 2024Copper Contributor
Doug_Robbins_Word_MVP Many thanks, Doug. It works very well with one issue. If there are no brackets left, it removes the first two characters in the document. Sorry for my ignorance but how do I insert a condition to Exit Sub if no bracket is found?
- Feb 01, 2024
inarbeth Use
Dim oRng As Range Set oRng = Selection.Range With oRng .Start = ActiveDocument.Range.Start .Start = .Start + InStrRev(.Text, "(") - 1 .Characters(1).Delete .End = ActiveDocument.Range.End If InStr(.Text, ")") <> 0 Then .End = .Start + InStr(.Text, ")") .Characters(.Characters.Count).Delete End If End With
- inarbethFeb 02, 2024Copper ContributorNot quite. If there are no brackets, the macro deletes the first character on the page.
- Feb 03, 2024
inarbeth Use a similar test
Dim oRng As Range Set oRng = Selection.Range With oRng .Start = ActiveDocument.Range.Start If InStr(.Text, "(") <> 0 Then .Start = .Start + InStrRev(.Text, "(") - 1 .Characters(1).Delete End If .End = ActiveDocument.Range.End If InStr(.Text, ")") <> 0 Then .End = .Start + InStr(.Text, ")") .Characters(.Characters.Count).Delete End If End With