几个参数功能VBA(尝试使用调用和括号,没有括号,仍然无法工作)

时间:2016-06-17 12:32:44

标签: regex string vba

我知道这会是我忽略的一些愚蠢的事情,但我发誓我不知道这段代码有什么问题:

Public Sub sustituirExpresion(ByVal Exp As String, ByVal Str As String, ByVal cell As String, ByVal hoja As String)
 Dim strPattern As String: strPattern = Exp
    Dim strReplace As String: strReplace = Str
    Dim regEx As New RegExp
    Dim strInput As String
    Dim Myrange As Range

    Set Myrange = Sheets(hoja).Range(cell)

    If strPattern <> "" Then
        strInput = Myrange.Value

        With regEx
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = strPattern
        End With

        sustituirExpresion = (regEx.Replace(strInput, strReplace))
    End If

End Sub
Sub limpiarDescripcion()
    Dim resultado As String

    resultado = sustituirExpresion "/s+", " ", "AD2", "Hoja1"
    MsgBox resultado


End Sub

我也尝试使用Call表单:

resultado = Call sustituirExpresion ("/s+", " ", "AD2", "Hoja1")

但它仍然给我一个错误“预期的功能或变量”,我无法理解为什么。

¿任何线索?

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

这里有几件事:

  • 将签名替换为Public Function sustituirExpresion(ByVal Exp As String, ByVal Str As String, ByVal cell As String, ByVal hoja As String) As String,将Sub变为Function

  • 您不需要.MultiLine = True,请保留False,因为您的正则表达式/\s+不包含任何^$来重新定义行为(如果您将Multiline设置为 True ,则会匹配行的开头/结尾

  • 您不需要regEx.Replace(strInput, strReplace)周围的括号,请使用sustituirExpresion = regEx.Replace(strInput, strReplace)