PDA

View Full Version : Help to run macro faster



Rakesh
06-07-2015, 10:44 AM
Hi Guys,

Like the below example I had a macro with more than hundred of normal find replace and wildcard replace.
It takes 5 to 8 minutes to run.

Is there a way to run the macro faster?


Sub test()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "aaa"
.Replacement.Text = "bbb"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "ccc"
.Replacement.Text = "dd"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll


ActiveDocument.Content.Find.Execute _
FindText:="^13([a-z])", _
MatchWildcards:=True, _
ReplaceWith:=" \1", _
Replace:=wdReplaceAll

ActiveDocument.Content.Find.Execute _
FindText:="( [a-z]@)^13([A-Z])", _
MatchWildcards:=True, _
ReplaceWith:="\1 \2", _
Replace:=wdReplaceAll

End Sub

Thanks,
Rakesh

gmayor
06-07-2015, 11:22 PM
Replace is what it is. With a lot of replacements it will take a long time to run. Console yourself with thinking about how long it would take without a macro. You could use the method at
http://www.gmayor.com/word_vba_examples_4.htm - "Replace a list of words from a table" without the ASK statements, and modified to use wildcards, but whether it would be any faster is debatable.