VBA函数返回字符串类型不匹配错误

时间:2017-03-24 03:23:21

标签: excel string vba excel-vba

我正在VBA for Excel 2010中编写一个宏。我觉得我在理解中遗漏了一些基本内容。我填充了一个字符串数组并迭代该数组中的内容。数组中的文本来自html格式的电子邮件消息,并且有大量不间断的空格。我想删除这些和其他非打印字符,以便我的输出只包含文本。

    For Each str In stringArr()
        CleanString (str)
        If Len(str) <> 0 Then
            Cells(Row + 1, 6 + index) = str
            index = index + 1
        End If            
    Next str

CleanString()返回一个字符串作为返回类型,但str不会更改。我试过了:

testString = cleanString(str)

,它给出了类型不匹配错误。有什么建议吗?

更多信息......

Function cleanString(sInput As String) As String
    Dim sResult As String
    sResult = sInput
    sResult = Replace(sResult, Chr(10), "")
    sResult = Replace(sResult, Chr(13), "")
    sResult = Replace(sResult, Chr(9), "")
    sResult = Replace(sResult, ChrW(160), "")
    sResult = Replace(sResult, Chr(32), "")
    Trim$ (sResult)
    Application.WorksheetFunction.Clean (sInput)
    cleanString = sResult
    Exit Function

End Function

1 个答案:

答案 0 :(得分:3)

感谢您的帮助。 问题是指定cleanString()需要一个字符串。 将它更改为cleanString(作为Variant),意味着我可以:

str = cleanString(str)

感谢Variatus