This Code does not work

T

Telesphore

This Code does not work:

*********************************************************
Sub ReplaceNumbers0To9()
Dim vFindText As Variant
Dim vReplText As Variant
Dim sFindText As String
Dim sReplText As String
Dim i As Long

vFindText = Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "V/",
"R/")

vReplText = Array("", "", "", "", "", "", "", "", "", "", "", "")

For i = LBound(vFindText) To UBound(vFindText)
sFindText = vFindText(i)
sReplText = vReplText(i)
With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Format = True
.MatchCase = False
.Text = sFindText
.Replacement.Text = sReplText
.Execute Replace:=wdReplaceAll
End With
Next i

End Sub
********************************************
Thank you
 
K

Karl E. Peterson

Telesphore has brought this to us :
This Code does not work:

This Code does work:

*********************************************************
Sub MeaninglessChatter()
MsgBox "Did you have a question?", vbCritical "WTF?"
End Sub
*********************************************************

You Welcome.
 
F

Fumei2 via OfficeKB.com

Works for me if you make your array on a single line:

vFindText = Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "V/",
"R/")

Make sure the "R/" if on a separate line uses the underscore character.

vFindText = Array("0", "1", "2", "3", "4", "5", "6", _
"7", "8", "9", "V/", "R/")

Why have the second array of ""? This also "works".

Sub ReplaceNumbers0To9()
Dim vFindText As Variant
Dim i As Long

vFindText = Array("0", "1", "2", "3", "4", "5", "6", _
"7", "8", "9", "V/", "R/")

For i = LBound(vFindText) To UBound(vFindText)
With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Format = True
.MatchCase = False
.Text = vFindText(i)
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
Next i
End Sub


However, it does not work on some circumstances.

yadda 345 will not have anything happen. So what - exactly - does "not work"
mean for you?
 
D

Doug Robbins - Word MVP

Doesn't compile. Should be:

MsgBox "Did you have a question?", vbCritical, "WTF?"

<G, D & RVF>
--
Hope this helps,

Doug Robbins - Word MVP

Please reply only to the newsgroups unless you wish to obtain my services on
a paid professional basis.
 
T

Telesphore

Thank you

It works for the numbers 0 to 9, but not for the numbers 10 to 19, 20 to
29... and so on.

It does not work either when there is no space between a number and a
letter, for example: 6The man came...
 
K

Karl E. Peterson

Doesn't compile. Should be:

MsgBox "Did you have a question?", vbCritical, "WTF?"

<G, D & RVF>

LOL! And this, my friends, is why we don't debug code that's never
seen the inside of the IDE! :)
Hope this helps,

Yeah yeah... ;-)
 
G

Graham Mayor

You don't need an array for the replacements when they are all the same.
If you want to remove *all* numbers from your document plus V/ and R/ the
following will work

Dim vFindText As Variant
Dim i As Long
vFindText = Array("[0-9]", "V/", "R/")
For i = LBound(vFindText) To UBound(vFindText)
sFindText = vFindText(i)
With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
.Text = sFindText
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
Next i


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>



Telesphore said:
Thank you

It works for the numbers 0 to 9, but not for the numbers 10 to 19, 20 to
29... and so on.

It does not work either when there is no space between a number and a
letter, for example: 6The man came...
 
T

Telesphore

Thanks Graham

It works fantastic!

Telesphore


Graham Mayor said:
You don't need an array for the replacements when they are all the same.
If you want to remove *all* numbers from your document plus V/ and R/ the
following will work

Dim vFindText As Variant
Dim i As Long
vFindText = Array("[0-9]", "V/", "R/")
For i = LBound(vFindText) To UBound(vFindText)
sFindText = vFindText(i)
With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
.Text = sFindText
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
Next i


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
T

Tony Jollans

It works for the numbers 0 to 9, but not for the numbers 10 to 19, 20 to
29... and so on.

It does not work either when there is no space between a number and a
letter, for example: 6The man came...

Just so you know for next time ...

You have used ".MatchWholeWord = True", which pretty much does what it says,
and explains whay it only works as you describe.

--
Enjoy,
Tony

www.WordArticles.com

Telesphore said:
Thank you

It works for the numbers 0 to 9, but not for the numbers 10 to 19, 20 to
29... and so on.

It does not work either when there is no space between a number and a
letter, for example: 6The man came...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top