PDA

View Full Version : [SOLVED:] wildcard range separators



vkhu
09-14-2017, 05:55 AM
I'm writing a search & replace macro with this wildcard expression:
<[Hh]a[vesd]{1,}>
Basically, I want to find all forms of the word "have". The problem is that it will also find Hades, Hae, which are 2 names I have in the document.

First I tried to remove the capital "H" so it won't select the names, but then the capitalized "Have," "Had," etc. won't get selected. I then try to put commas in between the letters like so:

<[Hh]a[ve,s,d]{1,}>
But that doesn't work. Apparently, the commas are recognized as a character to find also. This is strange since the exclusion expression works with separators (eg. [!eg,19]). Does the range search use some other type of separator?

macropod
09-14-2017, 02:12 PM
The best you can do is:
Find = <[Hh]a[vesd]{1,2}>

Kilroy
09-18-2017, 04:33 AM
Maybe using an array might be better?


Sub FindReplaceTargets()
Dim range As range
Dim i As Long
Dim TargetList
TargetList = Array("Have", "have", "Had", "had", "Has", "has")
For i = 0 To UBound(TargetList)
Selection.WholeStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = TargetList(i)
.Replacement.Text = "What Word?"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Next
End Sub

Paul_Hossler
09-18-2017, 06:55 AM
If you have
.MatchCase = False then you don't need both "Have" and "have"

Kilroy
09-18-2017, 07:22 AM
Thanks Paul I noticed it after posting. I found the following code when researching how do to a 2D array. How do we implement an array find and replace like:


For j = 1 To UBound(arr)
strOrig = arr(j)
arr(j) = Replace(arr(j), "Have", "whatever")
If arr(j) <> strOrig Then
changeCount = changeCount + 1
End If
Next j