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
Dave Lett - 09 Feb 2005 19:42 GMT
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
> Hello,
>
[quoted text clipped - 25 lines]
>
> End Sub
Dave Lett - 09 Feb 2005 19:46 GMT
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
> Hello,
>
[quoted text clipped - 25 lines]
>
> End Sub
Greg - 09 Feb 2005 20:10 GMT
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?
Dave Lett - 09 Feb 2005 20:47 GMT
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
> Dave,
>
[quoted text clipped - 6 lines]
> Did you every go back and look at the code we worked on last weak about
> long search strings?
Greg Maxey - 09 Feb 2005 22:20 GMT
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

Signature
Greg Maxey/Word MVP
A Peer in Peer to Peer Support
> Greg,
> Yeah, I had a look at the code from last week, but I couldn't think
[quoted text clipped - 16 lines]
>> Did you every go back and look at the code we worked on last weak
>> about long search strings?
Helmut Weber - 10 Feb 2005 12:32 GMT
Hi Greg,
I hope you don't mind being called "submariner" every now and then,
Not
http://gregmaxey.mvps.org/Fing_Long_String.htm
but
http://gregmaxey.mvps.org/Find_Long_String.htm
Just in case somebody needs as much time as I needed
to find out the typo.
Greetings from Bavaria
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
Greg - 10 Feb 2005 13:47 GMT
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.