Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

https://stackoverflow.com/questions/27614643/regex-expression-to-check-if-there-are-any-special-characters-in-string-like

No Format
Sub test()

'''Special Character Section'''
Dim special_charArr() As String
Dim special_char As String

special_char = "!,@,#,$,%,^,&,*,+,/,\,;,:"
special_charArr() = Split(special_char, ",")
'''Special Character Section'''

char = "#"
For Each Key In special_charArr
     special = InStr(char, Key)
      If special = 1 Then
         MsgBox char & " = #"
      End If
Next
End Sub

You can use RegEx but I found the solution less robust since I wanted special use cases for individual special_chars:

No Format
Function IsValidName(strData As String) As Boolean
    Dim RE As Object, REMatches As Object

    Set RE = CreateObject("vbscript.regexp")
    With RE
        .MultiLine = False
        .Global = True
        .IgnoreCase = True
        .Pattern = "[^a-zA-Z0-9]"
    End With

    IsValidName = Not RE.Test(strData)
End Function