Strip Non-Numeric Characters from a string or range

G

Greg

Hello,

I have a macro that I now use for stipping out non-numerical characters
from a string (Thanks Andi Mayer). This works great, but I was
wondering if there is a function available that might simplify the
process so that it doesn't have to cycle through each charcter? I
found VAL(myStr), but this only pulls out the leading numeric
characters. Is there something else that I should be using?

Thanks.

Sub StripOutNonNumerics()
Dim oChrNum As Long
Dim i As Long
Dim myStr As String
Dim tmpStr As String

myString = Selection.Range.Text

For i = 1 To Len(myStr)
oChrNum = Asc(Mid(myStr, i, 1))
If oChrNum >= 48 And oChrNum <= 57 Then
tmpStr = tmpStr + Chr(oChrNum)
End If
Next i
myStr = tmpStr
Selection.Range.Text = myStr

End Sub
 
D

Dave Lett

Hi Greg,

You are
1) using the selection to define the string
2) cycling through each character of the string (i.e., the selection)
3) removing each non-numeric character from the string (i.e., the selection)
4) overwriting the selection with the string.

Therefore, you could just use a search and replace with wildcards to change
the string:

With Selection.Find
.Text = "[A-z]"
.ClearFormatting
.MatchWildcards = True
With .Replacement
.Text = ""
.ClearFormatting
End With
.Execute Replace:=wdReplaceAll
End With

If you want to remove other characters (e.g., punctuation), then you'll have
to include those characters in the Find.Text property.

HTH,
Dave
 
D

Dave Lett

Hi Greg,
In my previous post, I forgot to add that it's best to set the .Wrap
property to "wdFindStop". If you have it set to "wdFindContinue", then it
will remove all non-numeric characters from the document, even if they are
not part of the selection.
BTW, I think your line "myString = Selection.Range.Text" is supposed to be
"myStr = Selection.Range.Text"

HTH,
Dave
 
G

Greg

Dave,

Clever boy :) Why didn't I think of that? As soon as I saw you
solution I immediately thought, "Why not Text = [!0-9]? Works like a
charm.

Break Break

Did you every go back and look at the code we worked on last weak about
long search strings?
 
D

Dave Lett

Greg,
Yeah, I had a look at the code from last week, but I couldn't think of a way
to substantially improve it, so I left it alone. I was tempted, however, to
keep the oSourceDoc document open and compare replaceRng with the found
text. But, again, I couldn't convince myself that this would improve the
code.
I like [!0-9] much better because it clarifies your ultimate intent.

Dave
 
G

Greg Maxey

Dave,

Yes I think for the very limited applications that it is done enough. I
posted if you want to have a look see:
http://gregmaxey.mvps.org/Fing_Long_String.htm


--
Greg Maxey/Word MVP
A Peer in Peer to Peer Support

Dave said:
Greg,
Yeah, I had a look at the code from last week, but I couldn't think
of a way to substantially improve it, so I left it alone. I was
tempted, however, to keep the oSourceDoc document open and compare
replaceRng with the found text. But, again, I couldn't convince
myself that this would improve the code.
I like [!0-9] much better because it clarifies your ultimate intent.

Dave

Greg said:
Dave,

Clever boy :) Why didn't I think of that? As soon as I saw you
solution I immediately thought, "Why not Text = [!0-9]? Works like a
charm.

Break Break

Did you every go back and look at the code we worked on last weak
about long search strings?
 
G

Greg

Well I am one of those "sumariner" types, so I don't mind.

Thanks for pointing out the spelling error. Unfortunately I seem to
leave plenty of those lying about. I will correct it when I get home.
 

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