Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2005

Tip: Looking for answers? Try searching our database.

removing text from a string

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lucas Karpiuk - 21 Oct 2005 15:44 GMT
how would i go about removing anything from a string that isn't a number,
including spaces, punctuation, etc etc?

thanks in advance
Greg - 21 Oct 2005 16:22 GMT
Something like this:

Sub StripNonNumerics()
Dim oRng As Range
Dim oChar As Range
Dim tempString As String
Set oRng = Selection.Range

For Each oChar In oRng.Characters
 If Asc(oChar) > 47 And Asc(oChar) < 58 Then
   tempString = tempString + oChar
 End If
Next
Selection.Range = tempString
End Sub
Greg - 21 Oct 2005 16:30 GMT
Lucas,

I didn't do a very good job with my first reply.  You want to strip
nonnumerics from a string not a range.  Try:

Sub StripNonNumericsFromString()
Dim oString As String
Dim i As Long
Dim cleanString As String
oString = "abc123efg"
For i = 1 To Len(oString)
 If Asc(Mid(oString, i, 1)) > 47 And Asc(Mid(oString, i, 1)) < 58 Then
   cleanString = cleanString + (Mid(oString, i, 1))
 End If
Next
MsgBox cleanString
End Sub
Lucas Karpiuk - 21 Oct 2005 16:37 GMT
Greg,

Actually I didn't word myself properly, I do indeed intend to use this on
selected text - and it works wonderfully, btw.

Thanks so much!
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.