正则表达式替换不起作用(错误445)

时间:2014-08-06 18:23:37

标签: vba excel-vba excel

首先,我使用Office 2010

我有vba脚本:

Function RegExpReplace(ByVal WhichString As String, _
                    ByVal Pattern As String, _
                    ByVal ReplaceWith As String, _
                    Optional ByVal IsGlobal As Boolean = True, _
                    Optional ByVal IsCaseSensitive As Boolean = True) As String
    'Declaring the object
    Dim objRegExp As Object
    'Initializing an Instance
    Set objRegExp = CreateObject("VBScript.RegExp")
    'Setting the Properties
    objRegExp.Global = IsGlobal
    objRegExp.Pattern = Pattern
    objRegExp.IgnoreCase = Not IsCaseSensitive
    'Execute the Replace Method
    RegExpReplace = objRegExp.Replace(WhichString, ReplaceWith)

End Function

我用sub测试了它:

Sub abc()
    Dim x
    x = RegExpReplace("123 foo 456 bar", "\d", "#")
End Sub

但是我在

的行处得到错误445(对象不支持)
RegExpReplace = objRegExp.Replace(WhichString, ReplaceWith)
你告诉我什么错了吗?

我必须使用后期绑定。

谢谢。

1 个答案:

答案 0 :(得分:0)

您可能需要 Microsoft VBScript正则表达式库参考:

enter image description here

另见下面的链接:
http://mark.biek.org/blog/2009/01/regular-expressions-in-vba/