PDA

View Full Version : Skipping Array Terms



dj44
02-25-2018, 07:43 AM
Good day folks,

I wanted to understand some logic about array terms

It’s a bit back wards but I wanted to see how to do this

Now if the Replace term = "SKIP" completely skip the search term and its execution



Sub Skip_Array_Terms()


Dim oRng As Word.Range
Dim arrWords As Variant
Dim oReplace As Variant
Dim i As Long, j As Long


arrWords = Array("A", "B", "C", "D")

oReplace = Array("Apple", "SKIP", "Cat", "SKIP")

For i = 0 To UBound(arrWords)
Set oRng = ActiveDocument.Range
j = 0
With oRng.Find
Do While .Execute(FindText:=arrWords(i), MatchWholeWord:=True)


'IF oReplace(i)="SKIP" << something like this?

oRng = oReplace(i)

oRng.Collapse 0
Loop
End With
Next i
Set oRng = Nothing

End Sub







What are the ways I may be able to achieve something like this, if it’s the same word then it is easier a clause somehwere?

I dont know if i would need to split the array as its the same word on the replace i would like to skip

gmayor
02-25-2018, 08:18 AM
I think what you are looking for is


Sub Skip_Array_Terms()
Dim oRng As Word.Range
Dim arrWords As Variant
Dim oReplace As Variant
Dim i As Long

arrWords = Array("A", "B", "C", "D")
oReplace = Array("Apple", "SKIP", "Cat", "SKIP")

For i = 0 To UBound(arrWords)
If Not UCase(oReplace(i)) = "SKIP" Then
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(FindText:=arrWords(i), MatchWholeWord:=True)
oRng = oReplace(i)
oRng.Collapse 0
Loop
End With
End If
Next i
Set oRng = Nothing
End Sub


Note that access to the Arrays is case sensitive.

dj44
02-25-2018, 08:42 AM
Hello Graham,

Nice to see you.

Oh yes this is the exact simple line that makes it easy for me to understand.




If Not UCase(oReplace(i)) = "SKIP" Then



Normally you skip terms on the search, but I didn’t want to split my arrays every time I had to skip a replacement, so it had to be backwards.

Now let me play about with this, because I do have a feeling seeing how useful it is

I may make an array of terms in the replacement to skip. eg




oSkipTerms = Array("SKIP1", "SKIP2")

If Not UCase(oReplace(i)) = oSkipTerms(j) Then

So it will be array within array


I'll pop back later with my new update

Good Sunday Graham