PDA

View Full Version : [SOLVED:] Search and Replace from the same Array



dj44
11-12-2017, 03:06 PM
hello folks and evening,

well i was thinking if it was possible to search and replace from the same array?



Sub SearchReplaceArray()
Dim oRng As Word.Range
Dim arrWords As Variant
Dim i As Long, j As Long
arrWords = Array(A, B, C, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
For i = 0 To UBound(arrWords)
Set oRng = ActiveDocument.Range
j = 0
With oRng.Find
Do While .Execute(FindText:=arrWords(i), MatchWholeWord:=True)
oRng = oReplace(i + 2)
oRng.Collapse 0
i = i + 1
Loop
End With
Next i
Set oRng = Nothing
End Sub


is this at all possible, if i have one array and i want to replace using the same array

example

A,B,C

becomes

1,2,3

macropod
11-12-2017, 04:12 PM
Try:

Sub Demo()
Application.ScreenUpdating = False
Dim StrFnd As String, i As Long
StrFnd = "0,1,2,3,4,5,6,7,8,9"
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
For i = 0 To UBound(Split(StrFnd, ","))
.Text = Split(StrFnd, ",")(i)
.Replacement.Text = Chr(Asc(Split(StrFnd, ",")(i)) + 17)
.Execute Replace:=wdReplaceAll
Next
End With
Application.ScreenUpdating = True
End Sub

dj44
11-12-2017, 04:29 PM
Hello Paul,

thank you for the speedy response :)



Now - what if my array has words in it


oSearch = Array("Green", "Grizzly", "Bob","Apple", "Bear", "Cat")

Could i substitute words within the same array

Replace > Green with Apple

Grizzly with Bear

Bob with Cat


with i+2 on the replace

I know its easier to use 2 arrays, but i am really curious becuase i wanted to add some stuff to the end of an array, and just do my replacement just for convenience really.

there is something i cant work out on the logic, you can search and replace 1-1 on the same terms but ... :think:


thanks for the code and help

macropod
11-12-2017, 04:36 PM
That's a disaster waiting to happen. What do you suppose will happen when 'Apple' is found, as it inevitably will be once you've replaced 'Green' with 'Apple'? Hint: 'out of bounds'.

macropod
11-12-2017, 04:50 PM
You might do better with something like:

Sub Demo()
Application.ScreenUpdating = False
Dim ArrFnd(), i As Long
ArrFnd = Array("Green", "Apple", "Grizzly", "Bear", "Bob", "Cat")
If UBound(ArrFnd) Mod 2 = 0 Then
MsgBox "Dodgy Array"
Exit Sub
End If
With ActiveDocument.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:=wdReplaceAll
Next
End With
Application.ScreenUpdating = True
End Sub
With this approach, each Find term is followed by its Replace term and the loop is processed in terms of item pairs.

dj44
11-12-2017, 04:51 PM
Well i can make a mess and it wouldnt be the first time at all :grinhalo:

Well i was trying to partition my array into sections

so i would run the loop between sections

for example
My search terms > 0 - 3 "Apple" "Grizzly" etc
My replace terms > 4 -7 "Green" "Bear"


i was trying to work out if i could partition my array - that would allow me to search and replace using the same source?

I am really speculating as i have not found anything yet to model this on

dj44
11-12-2017, 04:55 PM
Thank you Paul, it is making sense now, I could do that

dj44
11-13-2017, 04:29 PM
I have also made the basic example as like this



Sub Search_Replace_Same_Array()
Dim oRng As Word.Range
Dim oArray As Variant
Dim i As Long
oArray = Array("Search Terms >>>", "AA", "BB", "CC", "DD", "EE", "Replacement Terms put here >>> ", 1, 2, 3, 4, 5, 6, 7, 8, 9)
For i = 1 To 5
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(FindText:=oArray(i), MatchWholeWord:=True)
oRng = oArray(i + 6)
oRng.Collapse 0
Loop
End With
Next i
Set oRng = Nothing
End Sub



but i am happy that 1 array can be used to do the search and the replacements, and i never knew that before

Well i had a lot of arrays and i wnated to declutter my house so i thought why not use 1 array, if it does the job on the tin

thank you Paul