Happy New Year Everyone!
I am monkeying around with some code written by Peter Albert.
I wish to extend the ability to not only extract specified categories (alpha, numeric, and alpha-numeric)(This is presently accomplished by passing the optional parameter "strLimit"), but also to be able to be fed additional special characters in any given call.
The first part was easy by simply implementing an additional parameter and If/End structure such as:
Function AlphaNumericOnly(strSource As String, Optional strLimit As String) As String
'Based on original code by Peter Albert at http://stackoverflow.com/questions/15723672/how-to-remove-all-non-alphanumeric-characters-from-a-string-except-period-and-sp
' If Optional strLimit is missing or is "an" then returns both alpha and numeric
' If Optional strLimit is an "a" then returns strictly alpha
' If Optional strLimit is an "n" then returns strictly numeric
' If Optional strLimit is an "d" then returns numeric and a slash for dates
Dim i As Integer
Dim strResult As String
If bPubSkip Then 'To allow for not processing when called for the last time by a CHANGE event after an EXIT event:
AlphaNumericOnly = strSource
Else
If strLimit = "an" Or IsMissing(strLimit) Or strLimit = "" Then ' Alpha and Numeric
For i = 1 To Len(strSource)
Select Case Asc(Mid(strSource, i, 1))
Case 48 To 57, 65 To 90, 97 To 122:
strResult = strResult & Mid(strSource, i, 1)
End Select
Next
ElseIf strLimit = "a" Then ' Alpha
For i = 1 To Len(strSource)
Select Case Asc(Mid(strSource, i, 1))
Case 65 To 90, 97 To 122:
strResult = strResult & Mid(strSource, i, 1)
End Select
Next
ElseIf strLimit = "n" Then 'Numeric
For i = 1 To Len(strSource)
Select Case Asc(Mid(strSource, i, 1))
Case 48 To 57, 44, 46:
strResult = strResult & Mid(strSource, i, 1)
End Select
Next
ElseIf strLimit = "N" Then 'Numeric & hyphen ONLY
For i = 1 To Len(strSource)
Select Case Asc(Mid(strSource, i, 1))
Case 48 To 57:
strResult = strResult & Mid(strSource, i, 1)
End Select
Next
AlphaNumericOnly = strResult
ElseIf strLimit = "d" Then 'Numeric & slash ONLY
For i = 1 To Len(strSource)
Select Case Asc(Mid(strSource, i, 1))
Case 47 To 57:
strResult = strResult & Mid(strSource, i, 1)
End Select
Next
End If
AlphaNumericOnly = strResult
End If
End Function
In an effort to convert it to the feed of specific characters, my first step was to try to feed a string variable to the Case, first based upon the major category I wanted to extract (strLimit). Since Case takes literal parameters, I attempted to use Evaluate, but that failed with a "Type Mismatch" error:
Function TestOnly(strSource As String, Optional strLimit As String, Optional strExceptions As String) As String
' If Optional strLimit is missing or is "an" then returns both alpha and numeric
' If Optional strLimit is an "a" then returns strictly alpha
' If Optional strLimit is an "n" then returns strictly numeric
Dim i As Integer
Dim strCase As String
Dim strResult As String
If strLimit = "an" Then
strCase = "48 To 57, 65 To 90, 97 To 122:"
ElseIf strLimit = "a" Then
strCase = "65 To 90, 97 To 122:"
ElseIf strLimit + "n" Then
strCase = "48 To 57, 44, 46:"
End If
For i = 1 To Len(strSource)
Select Case Asc(Mid(strSource, i, 1))
Case [strCase]
strResult = strResult & Mid(strSource, i, 1)
End Select
Next
TestOnly = strResult
End Function
The train of thought was that if I can feed a string that I can build upon to CASE, then I could concatenate in to strCase the additional ASCII codes which I would build off of a third parameter sent to this function, which would be parsed into codes (strExceptions).
Is there a way that my attack can be accomplished, or need I work on another direction? I do have an ugly solution that calls for an If/End control and provides for a different literal string for every combination of Major Categories plus other characters I might wish to include (the start of which you see in the first example above), but as the variations grew I wanted to make the code adapt instead of adding another "ElseIf" each time I wanted to address yet another character combination!
I regret that my ability to conceive of variations to experiment with are limited by my understanding when faced with the Select Case structure taking literal parameters and not variables. I have researched the internet for a bit but could not yet find a solution.
Thank-you for considering my problem!
Bookmarks