Share via

Errors using VBScript_RegExp_55.RegExp's Replace method

Anonymous
2017-09-16T04:02:04+00:00

I am following a few online examples of how to use regular expressions to replace text in VBA:

http://msdn.microsoft.com/en-us/library/xwewhkd1(v=vs.110).aspx

http://stackoverflow.com/questions/5539141/microsoft-office-access-like-vs-regex

http://technet.microsoft.com/en-us/library/2008.05.heyscriptingguy.aspx?f=255&MSPPError=-2147217396

http://www.macrostash.com/2011/10/08/simple-regular-expression-tutorial-for-excel-vba/#codesyntax\_5

Here is my first crack at it:

   Function xpandProvince( inStr As String ) As String

      Dim regEx As New VBScript_RegExp_55.RegExp

      Dim Matches 'I don't really use this (yet)

      regEx.IgnoreCase = False

      regEx.Global = True

      regEx.MultiLine = True

      regEx.Pattern = "\bAB\b"

      xpandProvince = regEx.Replace( inStr, "Alberta" )

      regEx.Pattern = "\bBC\b"

      xpandProvince = regEx.Replace( inStr, "British Columbia" )

      regEx.Pattern = "\bMB\b"

      xpandProvince = regEx.Replace( inStr, "Manitoba" )

      regEx.Pattern = "\bNB\b"

      xpandProvince = regEx.Replace( inStr, "New Brunswick" )

      regEx.Pattern = "\bNL\b"

      xpandProvince = regEx.Replace( inStr, "Newfoundland" )

      regEx.Pattern = "\bNS\b"

      xpandProvince = regEx.Replace( inStr, "Nova Scotia" )

      regEx.Pattern = "\bNT\b"

      xpandProvince = regEx.Replace( inStr, "Northwest Territories" )

      regEx.Pattern = "\bNU\b"

      xpandProvince = regEx.Replace( inStr, "Nunavut" )

      regEx.Pattern = "\bON\b"

      xpandProvince = regEx.Replace( inStr, "Ontario" )

      regEx.Pattern = "\bPE\b"

      xpandProvince = regEx.Replace( inStr, "Prince Edward Island" )

      regEx.Pattern = "\bPEI\b"

      xpandProvince = regEx.Replace( inStr, "Prince Edward Island" )

      regEx.Pattern = "\bQC\b"

      xpandProvince = regEx.Replace( inStr, "Quebec" )

      regEx.Pattern = "\bSK\b"

      xpandProvince = regEx.Replace( inStr, "Saskatchewan" )

      regEx.Pattern = "\bYT\b"

      xpandProvince = regEx.Replace( inStr, "Yukon" )

   End Function 'xpandProvince

I think I've followed all the details in the webpage examples above, but Access's VBA IDE  reports "Compile error: Syntax error" for all of the regEx.Replace invocations.  I'd appreciate any advice on how to get this working.

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2017-09-16T05:59:22+00:00

InStr is a built-in function, so it is not suitable as the name for the argument of the function.

You overwrite xpandProvince each time, instead of accumulating the changes.

Function xpandProvince(InputString As String) As String

    Dim regEx As Object

    Dim Matches 'I don't really use this (yet)

    Dim ReturnString As String

    Set regEx = CreateObject("VBScript.RegExp")

    regEx.IgnoreCase = False

    regEx.Global = True

    regEx.MultiLine = True

    ReturnString = InputString

    regEx.Pattern = "\bAB\b"

    ReturnString = regEx.Replace(ReturnString, "Alberta")

    regEx.Pattern = "\bBC\b"

    ReturnString = regEx.Replace(ReturnString, "British Columbia")

    regEx.Pattern = "\bMB\b"

    ReturnString = regEx.Replace(ReturnString, "Manitoba")

    regEx.Pattern = "\bNB\b"

    ReturnString = regEx.Replace(ReturnString, "New Brunswick")

    regEx.Pattern = "\bNL\b"

    ReturnString = regEx.Replace(ReturnString, "Newfoundland")

    regEx.Pattern = "\bNS\b"

    ReturnString = regEx.Replace(ReturnString, "Nova Scotia")

    regEx.Pattern = "\bNT\b"

    ReturnString = regEx.Replace(ReturnString, "Northwest Territories")

    regEx.Pattern = "\bNU\b"

    ReturnString = regEx.Replace(ReturnString, "Nunavut")

    regEx.Pattern = "\bON\b"

    ReturnString = regEx.Replace(ReturnString, "Ontario")

    regEx.Pattern = "\bPE\b"

    ReturnString = regEx.Replace(ReturnString, "Prince Edward Island")

    regEx.Pattern = "\bPEI\b"

    ReturnString = regEx.Replace(ReturnString, "Prince Edward Island")

    regEx.Pattern = "\bQC\b"

    ReturnString = regEx.Replace(ReturnString, "Quebec")

    regEx.Pattern = "\bSK\b"

    ReturnString = regEx.Replace(ReturnString, "Saskatchewan")

    regEx.Pattern = "\bYT\b"

    ReturnString = regEx.Replace(ReturnString, "Yukon")

    xpandProvince = ReturnString

End Function 'xpandProvince

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2017-09-16T16:41:36+00:00

    I would have used REPLACE if I could find a way to anchor the match to beginning and end of a word, even if there is no adjacent delimiter character (which I take to be any character that wouldn't be used in a variable name).  For example, if the 2-3 letter acronym was at the start or end of a string.  Is there a way to do this using REPLACE?

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2017-09-16T16:33:06+00:00

    In such a car there is little to be gained in using RegEx.  Why not simply use the Replace function?

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2017-09-16T06:44:08+00:00

    Doh!  Thanks!

    Was this answer helpful?

    0 comments No comments