首页 > 解决方案 > Excel VBA 返回“编译错误:预期:表达式”

问题描述

我正在尝试将一个值设置为另一个函数的返回值,但我陷入了这一行:

Set tempMergeValue = TypeRegxFunc(MyRange.Rows(iCounter).Columns(1).Value,'([^.]+)')

TypeRegxFunc 在这里

Function TypeRegxFunc(strInput As String, regexPattern As String) As String
    Dim regEx As New RegExp
    With regEx
        .Global = True
        .MultiLine = True
        .IgnoreCase = False
        .Pattern = regexPattern
    End With

    If regEx.Test(strInput) Then
        Set matches = regEx.Execute(strInput)
        TypeRegxFunc = matches(0).Value
    Else
        TypeRegxFunc = "not matched"
    End If

End Function

标签: excelvba

解决方案


您必须''用引号替换撇号"",否则 VBA 会将其解释为注释。

更改此行:

Set tempMergeValue = TypeRegxFunc(MyRange.Rows(iCounter).Columns(1).Value,'([^.]+)')

有了这个:

Set tempMergeValue = TypeRegxFunc(MyRange.Rows(iCounter).Columns(1).Value,"([^.]+)")

它会起作用的。

我希望这有帮助。


推荐阅读