View Single Post
 
Old 08-29-2022, 02:04 AM
maniek maniek is offline Windows 10 Office 2007
Novice
 
Join Date: Dec 2014
Posts: 3
maniek is on a distinguished road
Default

I understand - replace each variable twice.


This works for me:

Sub Demo() 'replace each variable twice
'https://www.msofficeforums.com/word-vba/49582-vba-replace-repeated-single-variable-variable-list.html
Application.ScreenUpdating = False
Dim StrRep As String, i As Long, k As Long: k = 1
StrRep = "red|white|blue" '##########

With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
'.Wrap = wdFindStop
.Wrap = wdFindContinue
.MatchWildcards = True
.Text = "<VAR>" '##############
.Execute

For i = 0 To UBound(Split(StrRep, "|")) '0
.Replacement.Text = Split(StrRep, "|")(i)

Do While .Found
.Execute Replace:=wdReplaceOne, Forward:=True 'we need to replace 2 consecutive occurances!
k = k + 1
If k > 2 Then 'only twice
Exit Do
End If
Loop
k = 1
Next

End With
Reply With Quote