Monday, September 30, 2013

Validation in VBA using Regular Expressions (RegEx)

Will keep appending the validations I use (and have to think/ research before using) in this post. 

'check if it is alpha numeric
 Dim theRegex As Object
 Set theRegex = CreateObject("VBScript.RegExp")
 With theRegex
      .MultiLine = False
      .Global = True
      .IgnoreCase = False
 End With
 theRegex.Pattern = "([^A-Z^a-z^0-9^\s])"

 Set MyMatches = theRegex.Execute(nameStrRefToCompare)

 If MyMatches.Count <> 0 Then
     MsgBox ("Can have spaces, text, and numbers only")
 Else
     'do something else
 End If

No comments: