Re: Trimming and clensing a string

Tech-Archive recommends: Speed Up your PC by fixing your registry



> Basically, is there an easy way of removing all non-numeric characters
from
> a string?

The following function will remove all non-numeric characters from a string:

Public Function NumericOnly(sSource As String) As String

Dim BytesIn() As Byte
Dim BytesOut() As Byte
Dim LenBytes As Integer
Dim IdxIn As Integer
Dim IdxOut As Integer

If Nz(sSource, "") = "" Then
NumericOnly = ""
Exit Function
End If

BytesIn() = StrConv(sSource, vbFromUnicode)
LenBytes = UBound(BytesIn)

ReDim BytesOut(0 To LenBytes)

For IdxIn = 0 To LenBytes
If BytesIn(IdxIn) > 47 And BytesIn(IdxIn) < 58 Then
BytesOut(IdxOut) = BytesIn(IdxIn)
IdxOut = IdxOut + 1
End If
Next

NumericOnly = StrConv(BytesOut, vbUnicode)

End Function

-LGC


.



Relevant Pages