1

The function allows the character listed (in this case)

Case 48 To 57, 65 To 90, 97 To 122:

Is it possible to change this to Remove the characters listed instead?

Thanks

Function AlphaNumericOnly(strSource As String) As String
    Dim i As Integer
    Dim strResult As String

    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
    AlphaNumericOnly = strResult
End Function
Community
  • 1
  • 1
xyz
  • 2,253
  • 10
  • 46
  • 68
  • 1
    They both came from here http://stackoverflow.com/questions/15723672/how-to-remove-all-non-alphanumeric-characters-from-a-string-except-period-and-sp – xyz Aug 25 '15 at 15:11

1 Answers1

1

If you intend to use For ... Next - just add Case Else:

    For i = 1 To Len(strSource)
        Select Case Asc(Mid(strSource, i, 1))
            Case 48 To 57, 65 To 90, 97 To 122:
            Case Else:
                strResult = strResult & Mid(strSource, i, 1)
        End Select
    Next

As @pnuts pointed and @brettdj answered - RegEx is more efficient, in your case the function may be as follows:

Function NonAlphaNumericOnly(strSource As String) As String
    With CreateObject("VBScript.RegExp")
        .Global = True
        .IgnoreCase = True
        .Pattern = "[\w]+"
        NonAlphaNumericOnly = .Replace(strSource, "")
    End With
End Function
Community
  • 1
  • 1
omegastripes
  • 12,351
  • 4
  • 45
  • 96