Dave T
03-15-2018, 06:25 AM
Hello All,
Some time ago Greg Maxey helped me out with a find and replace macro that operates within a selected range.
I wanted to insert a tab space after a right parenthesis and found a Cybertext article that uses wildcards to find virtually what I was after:
https://cybertext.wordpress.com/2015/07/14/word-wildcard-find-and-replace-for-numbers-and-trailing-punctuation/
Using most of Greg’s macro I have modified this to search for a number followed by a right parentheses using a wildcard search based on the Cybertext article.
If for example, using the ‘quick brown fox’ text below, I select the first three paragraphs it works fine. However, if I select just the paragraphs with the numbers and right parenthesis it does not inset a tab after the number 1 as the end paragraph mark in the first paragraph was not included in the initial selected range.
The quick brown fox jumps over the lazy dog.
1) The quick brown fox jumps over the lazy dog.
2) The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
What changes do I need to make to the following macro to include the end paragraph mark of the previous paragraph that was not included in the initial selection ???
Sub FindRightParenthesisWithinSelection()
'Code written by Greg Maxey
Dim myRange As Range
Dim oRngBounds As Range
Set myRange = Selection.Range
Set oRngBounds = myRange.Duplicate
With myRange.Find
'Find end paragraph mark then number then right ) followed by a space
.Text = "(^013)([0-9]@)([\)])([ ])"
.wrap = wdFindStop
.MatchWildcards = True
.Forward = True
Do While .Execute
If Not myRange.InRange(oRngBounds) Then Exit Sub
myRange.Select
ActiveWindow.ScrollIntoView Selection.Range
Select Case MsgBox("Do you want to insert a tab after this bracket?", vbQuestion + vbYesNoCancel, "Insert tab after bracket")
Case vbYes
myRange.Characters.Last = vbTab
Case vbNo
'Do nothing.
Case Else
GoTo lbl_Exit
End Select
myRange.Collapse wdCollapseEnd
Loop
End With
lbl_Exit:
Exit Sub
End Sub
Regards,
Dave T
Some time ago Greg Maxey helped me out with a find and replace macro that operates within a selected range.
I wanted to insert a tab space after a right parenthesis and found a Cybertext article that uses wildcards to find virtually what I was after:
https://cybertext.wordpress.com/2015/07/14/word-wildcard-find-and-replace-for-numbers-and-trailing-punctuation/
Using most of Greg’s macro I have modified this to search for a number followed by a right parentheses using a wildcard search based on the Cybertext article.
If for example, using the ‘quick brown fox’ text below, I select the first three paragraphs it works fine. However, if I select just the paragraphs with the numbers and right parenthesis it does not inset a tab after the number 1 as the end paragraph mark in the first paragraph was not included in the initial selected range.
The quick brown fox jumps over the lazy dog.
1) The quick brown fox jumps over the lazy dog.
2) The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
What changes do I need to make to the following macro to include the end paragraph mark of the previous paragraph that was not included in the initial selection ???
Sub FindRightParenthesisWithinSelection()
'Code written by Greg Maxey
Dim myRange As Range
Dim oRngBounds As Range
Set myRange = Selection.Range
Set oRngBounds = myRange.Duplicate
With myRange.Find
'Find end paragraph mark then number then right ) followed by a space
.Text = "(^013)([0-9]@)([\)])([ ])"
.wrap = wdFindStop
.MatchWildcards = True
.Forward = True
Do While .Execute
If Not myRange.InRange(oRngBounds) Then Exit Sub
myRange.Select
ActiveWindow.ScrollIntoView Selection.Range
Select Case MsgBox("Do you want to insert a tab after this bracket?", vbQuestion + vbYesNoCancel, "Insert tab after bracket")
Case vbYes
myRange.Characters.Last = vbTab
Case vbNo
'Do nothing.
Case Else
GoTo lbl_Exit
End Select
myRange.Collapse wdCollapseEnd
Loop
End With
lbl_Exit:
Exit Sub
End Sub
Regards,
Dave T