View Full Version : [SOLVED:] Is it possible to loop a find and replace array?
Kilroy
05-29-2018, 07:43 AM
I have this find and replace array but I need it to loop until no more of the "find" statements are found. Is this even possible?
'notready loop won't stop
SubFindReplaceMultiple()
'Do While??Until??
Application.ScreenUpdating= True 'False
Dim ArrFnd(), iAs Long
ArrFnd =Array("11", "1", “22”, “2”, "33", "3", “44”,"4", "55", "5")
IfUBound(ArrFnd) Mod 2 = 0 Then
MsgBox "Somethings Wrong Genious"
Exit Sub
End If
WithActiveDocument.range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
For i = 0 To UBound(ArrFnd) Step 2
.Text = ArrFnd(i)
.Replacement.Text = ArrFnd(i + 1)
'.Execute Replace:=wdFindContinue
.Execute Replace:=wdReplaceAll
Next
'End With
Application.ScreenUpdating= True
'Loop 'Until??
End With
End Sub
macropod
05-29-2018, 03:39 PM
Why do you need an array? More to the point, why do you need a macro? You could do the lot with a single wildcard Find/Replace, where:
Find = <([1-5])\1>
Replace = \1
Kilroy
05-30-2018, 04:35 AM
Thanks for the reply Paul. I should have specified that the numbers were just place holders in the array. The actual array contents will be to get rid of a number of different formatting things like 2 spaces to 1, space return to return, 2 tabs to one, 2 returns to 1 and so on. Just a number of formatting things. The way I have it written it for example it would find 2 spaces and change to one but if there is 3 spaces it will go down to 2 but will not loop back to see that there is sill an instance where 2 spaces still exist and so on for the other find replace pairs as well.
Is there a way to keep the find replace going until no more of the "find" are found? Loop?
macropod
05-30-2018, 04:53 AM
In that case:
Sub ArrFndRep()
Dim ArrFR As Variant, i As Long
ArrFR = Array("the", "quick", "brown", "fox", "jumped", "over", "lazy", "dog")
If UBound(ArrFR) Mod 2 = 0 Then
MsgBox "Something's Wrong Genious"
Exit Sub
End If
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Format = False
.MatchWholeWord = True
.MatchCase = False
.MatchWildcards = False
.Wrap = wdFindContinue
For i = 0 To UBound(ArrFR) Step 2
.Text = ArrFR(i)
.Replacement.Text = ArrFR(i + 1)
.Execute Replace:=wdReplaceAll
Next
End With
End Sub
As for:
find 2 spaces and change to one but if there is 3 spaces it will go down to 2
that's easily handled in a single pass, replacing any sequence of two spaces with a single space.
Kilroy
05-30-2018, 06:43 AM
Paul thanks for the quick reply. Here is the code you posted with my find and replace terms. Still doesn't work. Many instances not replaced.
Sub ArrFndRep()
Dim ArrFR As Variant, i As Long
ArrFR = Array(" ", " ", vbTab & vbTab, vbTab, " ^p", "^p", vbTab & "^p", "^p", "^p^p", "^p")
If UBound(ArrFR) Mod 2 = 0 Then
MsgBox "Something's Wrong Genious"
Exit Sub
End If
With ActiveDocument.range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Format = False
.MatchWholeWord = True
.MatchCase = False
.MatchWildcards = False
.Wrap = wdFindContinue
For i = 0 To UBound(ArrFR) Step 2
.Text = ArrFR(i)
.Replacement.Text = ArrFR(i + 1)
.Execute Replace:=wdReplaceAll
Next
End With
End Sub
Paul_Hossler
05-30-2018, 09:16 AM
Using Wildcards
Option Explicit
Sub ArrFndRep()
Dim ArrFR As Variant, i As Long
ArrFR = Array( _
" {2,}", " ", _
vbTab & "{2,}", vbTab, _
vbTab & "{2,}^0013", "^0013", _
" {1,}^0013", "^0013", _
"^0013{2,}", "^0013" _
)
If UBound(ArrFR) Mod 2 = 0 Then
MsgBox "Something's Wrong Genious" ' Like it :-)
Exit Sub
End If
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Format = False
.MatchWholeWord = True
.MatchCase = False
.MatchWildcards = True ' <<<<<<<<<<<<<<<<
.Wrap = wdFindContinue
For i = 0 To UBound(ArrFR) Step 2
.Text = ArrFR(i)
.Replacement.Text = ArrFR(i + 1)
.Execute Replace:=wdReplaceAll
Next
End With
End Sub
Kilroy
05-30-2018, 09:42 AM
Paul thanks for the reply. What the heck is "^0013"? It still isn't changing tab return to return.
Kilroy
05-30-2018, 10:24 AM
Ok I figured it out. in the second line I removed the {2, }. works great now. Thanks for the help fellas.
macropod
05-30-2018, 02:41 PM
Try:
Sub ArrFndRep()
Dim ArrFR As Variant, i As Long
ArrFR = Array(" ", " ", "[^t]{2,}", "^t", "[ ^t]{1,}^13", "^p", "[^13]{2,}", "^p")
If UBound(ArrFR) Mod 2 = 0 Then
MsgBox "Something's Wrong Genious"
Exit Sub
End If
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Format = False
.MatchWildcards = True
.Wrap = wdFindContinue
For i = 0 To UBound(ArrFR) Step 2
.Text = ArrFR(i)
.Replacement.Text = ArrFR(i + 1)
.Execute Replace:=wdReplaceAll
Next
End With
End Sub
When using wildcards:
.MatchWholeWord = True
.MatchCase = False
are invalid, so there's no point having them. You also don't need ^0013 - ^13 will do - and you should NOT use ^13 (or ^0013) as the Replace expression; otherwise the affected paragraphs will become one, joined together by what appears as a ¶ character. Having " {2,}" as a Find expression with " " as the Replace expression will reduce all space sequences to 1, whereas your previous post indicated you wanted sequences of 2 and 3 spaces reduced by 1 space each (meaning a 3-space sequence would be reduced to 2 spaces, not to 1 space).
Paul_Hossler
05-31-2018, 04:20 AM
Paul thanks for the reply. What the heck is "^0013"? It still isn't changing tab return to return.
That's the ASCII value for a CR - some MS Word special code (like ^p, and ^t) aren't recognized using wildcards
I wasn't sure about how to interpret some of the F&R's you wanted, so I sort of guessed at the {2,}, but I figured you could sort it out
'space'{2,} means 'two or more spaces'
'space'{2,5} means 'two to five spaces'
These constructs 'sort of' a carry over from Regular Expressions - online help is pretty good
BTW, IMHO it's worth your while to become a little familiar with MS Word's F&R wildcard option, since I've found that it is 1) powerful, and 2) can greatly simplify your macros
macropod
05-31-2018, 06:16 AM
some MS Word special code (like ^p, and ^t) aren't recognized using wildcards
^t is, though.
Kilroy
06-04-2018, 08:26 AM
Thanks for the information guys really appreciated. I tried running the new macro and I still end up with what looks like 2 return characters.
macropod
06-04-2018, 02:19 PM
With Word's formatting display turned on, are you seeing:
¶
¶
or something else, for example:
↵
¶
or:
↵
↵
If you're not seeing anything like that, it's probably just the paragraph before/after spacing.
Kilroy
06-05-2018, 05:22 AM
I'm seeing:
¶
¶
I tried removing space before and after settings before running and got the same result.
Kilroy
06-05-2018, 05:42 AM
I know I've marked this thread as solved and did so because ultimately the code you guys helped me with does resolve my current issues but, is it possible loop a find replace macro? or just a line of code that would check to se if anymore instance of the "find" are there before ending the macro?
macropod
06-05-2018, 05:50 AM
Unless there are other characters before the 2nd ¶ (e.g. non-breaking spaces), the Find/Replace macro I posted will take care of those.
macropod
06-05-2018, 05:55 AM
is it possible loop a find replace macro? or just a line of code that would check to se if anymore instance of the "find" are there before ending the macro?
That's what the macro I posted already does - and replaces them as specified.
Kilroy
06-05-2018, 06:03 AM
this is what it looks like:
It looks like 2 paragraph makers but even a separate find replace doesn't recognize two. wonder if the final paragraph marker in a document has the same characteristics?
macropod
06-05-2018, 06:13 AM
As I said:
Unless there are other characters before the 2nd ¶ (e.g. non-breaking spaces), the Find/Replace macro I posted will take care of those.
Without actually seeing the problem document, it's impossible to know for sure what other content might be between the two ¶ characters in your screenshot. Can you attach a document to a post with some representative data (delete anything sensitive)? You do this via the paperclip symbol on the 'Go Advanced' tab at the bottom of this screen.
Kilroy
06-05-2018, 06:32 AM
here it is:
Paul_Hossler
06-05-2018, 11:58 AM
You can't delete the last paragraph mark in a document. It looks like you have one empty paragraph in the document (the first ¶, and the document's final ¶)
You can use a macro I guess to see if a paragraph is empty and delete it if it is a real issue
Playing around -- I found it easier to just use subs instead of the array
Option Explicit
Sub ArrFndRep()
'2 or more spaces to one space
Call pvtWildcardFR(" {2,}", " ")
'2 or more tabs to one tab
Call pvtWildcardFR("^t{2,}", "^t")
'space or tab + para to one para
Call pvtWildcardFR("[ ,^t]{1,}^13", "^13")
'2 or more para to one para
Call pvtWildcardFR("^13{2,}", "^13")
End Sub
Private Sub pvtWildcardFR(Orig As String, Repl As String)
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Format = False
.MatchWholeWord = True
.MatchCase = False
.MatchWildcards = True
.Wrap = wdFindContinue
.Text = Orig
.Replacement.Text = Repl
.Execute Replace:=wdReplaceAll
End With
End Sub
Kilroy
06-05-2018, 12:04 PM
Very interesting approach. Thanks Paul
macropod
06-05-2018, 02:26 PM
If all you're worried about is that a final pair of paragraph breaks remains in your document after running the macro, that's not actually something Find/Replace can do anything about - because the final paragraph break cannot be processed. You'll find the same thing with the final paragraph before a table. They will be processed anywhere else in the document, however.
Kilroy
06-06-2018, 04:24 AM
Thanks Paul. No I'm definitely not worried about having the paragraph breaks remaining. I just thought it was interesting that the final break has different properties. Thanks again guy for the help.
Paul_Hossler
06-06-2018, 12:46 PM
Thanks Paul. No I'm definitely not worried about having the paragraph breaks remaining. I just thought it was interesting that the final break has different properties. Thanks again guy for the help.
Word stores a lot of information about the Paragraph at the paragraph mark position in the in the document file
IIRC, the last paragraph mark also includes Section information which is why it's 'special' and not deletable
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.