RegEx Errors

psycoperl

Active Member
Joined
Oct 23, 2007
Messages
338
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
  3. Web
When running a sub that calls the ValidateState function (see below) I get a VBA Error:
Run-time error '5017': Method 'Test' of object 'IRegExp2' failed

When I click on "debug" it highlights the line:
Code:
        ValidateState = .Test(sState)

However, I am not certain what is wrong.

Especially as the Function ValidateEmail which I used as the starting point for this function works... all I just did was change the regex string.

Thank you for your assistance.



Code:
Function ValidateState(ByVal sState As String) As Boolean
  'Function Source: http://www.msofficegurus.com/post/How-to-VBA-email-validation.aspx
  
    Dim oRegularExpression As RegExp
  
'   Sets the regular expression object
    Set oRegularExpression = New RegExp

    With oRegularExpression
'   Sets the regular expression pattern

'Original Pattern        .Pattern = "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"

'New Pattern from http://regexlib.com/RETester.aspx?regexp_id=471
    'The RE match U.S. state abbreviation used by the U.S. Post Office.
        .Pattern = "^(?-i:A[LKSZRAEP]|C[AOT]|D[EC]|F[LM]|G[AU]|HI|I[ADLN]|K[SY]|LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY])$"
  
'   Ignores case
        .IgnoreCase = True

'       Test State string
        ValidateState = .Test(sState)
    End With
    
End Function


Code:
Function ValidateEmail(ByVal sEmail As String) As Boolean
  'Function Source: http://www.msofficegurus.com/post/How-to-VBA-email-validation.aspx
  
    Dim oRegularExpression As RegExp
  
'   Sets the regular expression object
    Set oRegularExpression = New RegExp

    With oRegularExpression
'   Sets the regular expression pattern

'Original Pattern        .Pattern = "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"

'New Pattern from http://regexlib.com/RETester.aspx?regexp_id=269
        .Pattern = "^[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$"
  
'   Ignores case
        .IgnoreCase = True

'       Test email string
        ValidateEmail = .Test(sEmail)
    End With
    
End Function
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
What are you testing for exactly? What is this part (?-i: in the regular expression for?
 
Last edited:
Upvote 0
I think it is likely you are using a regex pattern for a different "flavor" of regex. VBScript uses what is basically a Javascript/JScript flavor - you might be using a pattern for .NET regex. Where did you get the pattern?


Side note: I've not seen the "IRegExp2" in an error message. What reference did you set?
 
Last edited:
Upvote 0
What are you testing for exactly? What is this part (?-i: in the regular expression for?

HotPepper,
Thank you for your response.

The expression is for "The RE match U.S. state abbreviation used by the U.S. Post Office."

I am not sure regarding the RegExp I got the string from: Regular Expression Library
 
Upvote 0
I think it is likely you are using a regex pattern for a different "flavor" of regex. VBScript uses what is basically a Javascript/JScript flavor - you might be using a pattern for .NET regex. Where did you get the pattern?


Side note: I've not seen the "IRegExp2" in an error message. What reference did you set?

HI Xenou, thank you for your assistance. I got the pattern from regexlib.com as noted in the comments in the code.

For the Reference I have set: Microsoft VBScript Regular Expressions 5.5
 
Upvote 0
I can't get the pattern to work on any regex testers - I think it's a bad one!
ξ
 
Upvote 0
@Xenou
Thank you... I created a new table and change the function that queries the table to take care of the validation... I was just trying to avoid another ADODB call.

Thanks again for your assistance!
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,243
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top