Regular Expression Problem

TFCJamieFay

Active Member
Joined
Oct 3, 2007
Messages
480
Hi All,

I'm having problems with the following bit of code.

Code:
Sub PostcodeChecker()

    Dim RgExp As Variant
    
    'Create the regular expression object
    Set RgExp = CreateObject("VBScript.RegExp")
    
    'Clear the function value
    IsUKPostCode = ""
    
    'Check we have value to test
    If MyPC = "" Then
        IsUKPostCode = "Not Supplied"
        msg = MsgBox("The postcode that you inputted is " & IsUKPostCode, vbOKOnly _
            + vbCritical)
        Exit Sub
    End If
    
    RgExp.Pattern = "(((^[BLMNS][1-9]\d?)|" _
        & "(^(AL|B[ABDHLNRS]|C[ABFHMORTVW]|D[AEHLNTY]|E[NX]|FY|G[LUY]|" _
        & "H[ADGPRUX]|I[GP]|KT|L[ADELNSU]|M[EK]|N[EGNPR]|O[LX]|" _
        & "P[ELOR]|R[GHM]|S[AEGKL-PRSTWY]|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)\d\d?)|" _
        & "(^E[1-9])|" _
        & "(^E1[W0-9])|" _
        & "(^EC[5]0)|" _
        & "(^EC[1][AMNRVY])|" _
        & "(^EC[2][AMNPRVY])|" _
        & "(^EC[3-4][AMNPRV])|" _
        & "(^N1[1-9])|" _
        & "(^NW1[W01])|" _
        & "(^NW[1-9])|" _
        & "(^SS[0-9])|" _
        & "(^SW1[AEHPVWXY0-9])|" _
        & "(^SW[2-9])|" _
        & "(^W[2-9])|" _
        & "(^W1[A-HJKSTUW0-4])|" _
        & "(^WC[1][ABEHNRVX])|" _
        & "(^WC[2][ABEHNR])" _
        & ")(\s*)?" _
        & "([0-9]))$|" _
        & "(^GIR\s?0AA$)"
    
    If RgExp.Test(MyPC) = True Then
        'Application.Run "FindBranch"
        MsgBox "MyPC = True"
    Else
        RgExp.Pattern = "(^G[1-9]\d?)|" _
            & "(^ZE|KW|HS|IV|AB|PH|DD|PA|FK|KY|ML|EH|KA|DG|IM|BT|GY|JE)\d\d?|" _
            & ")(\s*)?" _
            & "([0-9]))$|" _
            & "(^GIR\s?0AA$)"
        If RgExp.Test(MyPC) = True Then ' ---------error here
            MsgBox "Scottish or Non Mainland UK postcode"
        Else
            MsgBox "Invalid postcode"
        End If
    End If
    
End Sub

It all works fine until it get to here...

Code:
 If RgExp.Test(MyPC) = True Then ' ---------error here
            MsgBox "Scottish or Non Mainland UK postcode"

...then I get the following message...

Run time error '5017':
Application defined or object defined error


Is it because I need to clear/reset the regular expression before setting a new one or something? I'm not really very familiar with this function so any help would be greatly appreciated.

Many thanks,

Jay
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi Jay

It seems the second pattern is invalid.

Rich (BB code):
RgExp.Pattern = "(^G[1-9]\d?)|" _
    & "(^ZE|KW|HS|IV|AB|PH|DD|PA|FK|KY|ML|EH|KA|DG|IM|BT|GY|JE)\d\d?|" _
    & ")(\s*)?" _

Where's the opening parenthesis for the third closing parenthesis?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,695
Members
448,979
Latest member
DET4492

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