|
|
|
|
|
|
Multiple Apps
|
Function to Remove either All Numeric or All Non-Numeric Characters from a String
|
|
Ease of Use
|
Easy
|
Version tested with
|
2000, 2002
|
Submitted by:
|
matthewspatrick
|
Description:
|
This function will remove, at user's option, either all of the numeric characters or all of the non-numeric characters. These simple instructions may be overridden with specific characters that are either always or never allowed.
|
Discussion:
|
In the past, I have had to parse alphanumeric strings of varying length, in which the letters meant one thing and the numbers another. I could use formulas to grab just the characters I needed, but the character positions were not always constant, so sometimes the formulas failed.
This function would make that sort of task easier: for the given string, the function will either remove all non-numbers or remove all numbers. Further, you can override with certain letters. For example, the optional argument AllowedChar allows you to specify a string whose characters are always allowed. Thus, you could specify "$,." and allow the dollar sign, comma, and period to pass unmolested even if the function is told to remove non-numeric characters. The optional argument NeverAllow also sets up an override, this time of characters never allowed into the result string.
|
Code:
|
instructions for use
|
Option Explicit
Public Function StripOutCharType(CheckStr As String, Optional KillNumbers As Boolean = True, _
Optional AllowedChar As String, Optional NeverAllow As String)
Dim Counter As Long
Dim TestChar As String
Dim TestAsc As Long
For Counter = 1 To Len(CheckStr)
TestChar = Mid(CheckStr, Counter, 1)
TestAsc = Asc(TestChar)
If InStr(1, NeverAllow, TestChar, vbTextCompare) > 0 Then
ElseIf InStr(1, AllowedChar, TestChar, vbTextCompare) > 0 Then
StripOutCharType = StripOutCharType & TestChar
ElseIf KillNumbers Then
If TestAsc < 48 Or TestAsc > 57 Then
StripOutCharType = StripOutCharType & TestChar
End If
Else
If TestAsc >= 48 And TestAsc <= 57 Then
StripOutCharType = StripOutCharType & TestChar
End If
End If
Next
End Function
|
How to use:
|
- Paste the code above into a regular module in your project
- Use the function in your code, or in your application's user interface (such as in an Excel worksheet formula--see the attached file for examples--or in an Access query, form, or report)
|
Test the code:
|
- Try several iterations with the function, varying the argument settings.
-
- Please see the attached sample file for several examples of how to use this function.
|
Sample File:
|
StripOutCharTypeExample.zip 8.58KB
|
Approved by mdmackillop
|
This entry has been viewed 162 times.
|
|