How replace text with the contents of a variable

I can find and replace text using a macro but can't seem to figure out how to find text and replace it with the contents of a variable. I thought the following code would work but it doesn't. 'Plane' is the variable and 'Count' is a For-Next loop. So the first pass the replacement text would be the contents of Plane1, then Plane2, then Plane3 etc
.Replacement.Text = Plane & Count

Open in new window

agregsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
If you want to replace every occurrence of some text, then you don't need a VBA loop. You can just need to use something like this:

With ActiveDocument.Range.Find
      .Text = "Some Text"
      .Replacement.Text = Plane
      .Execute Replace:=wdReplaceAll
End With
0
agregsonAuthor Commented:
Thanks Graham but that isn't what I need. Basically what is happening is this:

Find "[text" & Count & "]" (so on first loop this is [text1])
Insert contents of filename:="file" & Count & ".txt"
Then find [MoreText] and replace with the contents of Plane & Count (so on first loop this is Plane1)

This will then loop until the For-Next is finished.

Hope this makes sense.
0
GrahamSkanRetiredCommented:
Perhaps you need something like this:
Sub FindLoop()
Dim Count As Integer
Dim Limit As Integer
Dim rng As Range
Limit = 100
For Count = 1 To Limit
    Set rng = ActiveDocument.Range
    With rng.Find
        .Text = "[text" & Count & "]"
        If .Execute(Replace:=wdReplaceNone) Then
            rng.InsertFile "file" & Count & ".txt"
            rng.End = ActiveDocument.Range.End
            .Text = "More Text"
            .Replacement.Text = "Plane" & Count
            If .Execute(Replace:=wdReplaceOne) Then
            Else
                MsgBox """More Text"" not found after  ""[text" & Count & "]"""
                Exit Sub
            End If
        Else
            MsgBox """[text" & Count & "]"" not found"
            Exit Sub
        End If
    End With
Next Count
End Sub

Open in new window

0
agregsonAuthor Commented:
Thanks Graham but I think this is the line I'm having problems with

.Replacement.Text = "Plane" & Count

Everytime the code loops this will replace text with Plane1, Plane2 etc, but Plane1, Plane2 etc are variables that could contain anything e.g.
Plane1 = Monitor
Plane2 = Hair Cut
Plane3 = 5050

So I need to be able to replace the text with the contents of the variable and not the variable name. Hope this makes sense.
0
agregsonAuthor Commented:
Turns out I just had to create an array and use the following code

        .Replacement.Text = LinesVar(Count)

where Count is just the counter in a For-Next loop.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.