VBA函数从字符串输入返回数组

时间:2013-04-15 14:33:34

标签: arrays string function vba split

我编写了一个函数来进行一些基本的字符串解析,但由于我目前不知道的原因,我的编译器不断标记我的代码以查找类型不匹配错误。该函数接受一个字符串,用空格替换一些字符,然后使用拆分将输出转换为数组。然后将该函数设置为输出数组。运行时,函数完成其进程,但编译器会在调用模块中抛出类型不匹配错误。代码如下,请提前感谢您的协助。

Sub Tester()

Dim TestArr()
Dim TestVar As String

Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
Application.ScreenUpdating = False

TestVar = "Text-with/characters I need to\remove"

Debug.Print InputParser(TestVar)

Application.Calculation = xlCalculationAutomatic
Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub

“---------------------------------------------- -------------------------------------------------- -----------------------------------

Function InputParser(InputStr As String)

Dim OutputArr() As String

If InStr(1, InputStr, "/", vbBinaryCompare) >= 1 Or _
    InStr(1, InputStr, "\", vbBinaryCompare) >= 1 Or _
    InStr(1, InputStr, "-", vbBinaryCompare) >= 1 Or _
    Left(InputStr, 1) = "'" Then.

        InputStr = Replace(InputStr, "/", " ", 1, , vbBinaryCompare)
        InputStr = Replace(InputStr, "\", " ", 1, , vbBinaryCompare)
        InputStr = Replace(InputStr, "-", " ", 1, , vbBinaryCompare)
        InputStr = Replace(InputStr, "'", vbNullString, 1, 1, vbBinaryCompare)
        OutputArr = Split(InputStr, " ", Compare:=vbBinaryCompare)
        Debug.Print Join(OutputArr, vbCrLf)
        Debug.Print TypeName(OutputArr)
End If

InputParser = OutputArr
Debug.Print TypeName(InputParser)

End Function

2 个答案:

答案 0 :(得分:1)

问题是当您使用Debug.Print尝试查看函数的结果时。

你不能debug.print一个数组!

否则,Jonners提供的调整功能可以正常工作。

如果需要查看返回数组中的内容,则需要在创建数组后设置断点,然后使用Locals窗口进行检查。

另外,如果没有任何可用的东西,Split会恼人地返回NULL。我创建了SmartSplit而不是Split。如果没有发生实际拆分,SmartSplit会返回数组中的原始字符串。这意味着您在使用Split后无需测试数组。

'------------------------------------------------------------------------------------------
'Procedure : SmartSplit
'Date : 2 March 2011
'Author : Nick Pullar
'Purpose : Enhances Split by outputting an array of one element if the strDelimiter is not found in strString
'Arguments : strString: The string that needs to be split
' strDelimiter: the string what holds the delimiter
'------------------------------------------------------------------------------------------
Function SmartSplit(ByVal strString As String, ByVal strDelimiter As String) As Variant
'Splits the string by strDelimiter using Split if at least one instance of strDelimiter is found in strString
'Otherwise just returns strString as an array as would be the result if Split had done its work
    Dim varResult As Variant

    If InStr(strString, strDelimiter) = 0 Then
        ReDim varResult(0)
        varResult(0) = strString
    Else
        varResult = Split(strString, strDelimiter)
    End If
    SmartSplit = varResult
End Function

我希望这会有所帮助。

尼克

答案 1 :(得分:0)

首先,你不需要围绕'replace'进行条件测试 - 如果输入字符串不包含目标字符,Replace将不会做任何事情。

其次,只有在实际替换某些内容时才会填充输出数组;在字符串不包含任何需要替换的内容的情况下,您的函数返回一个'null'数组。

调整功能:

Function InputParser(InputStr As String)
  Dim OutputArr() As String

  InputStr = Replace(InputStr, "/", " ", 1, , vbBinaryCompare)
  InputStr = Replace(InputStr, "\", " ", 1, , vbBinaryCompare)
  InputStr = Replace(InputStr, "-", " ", 1, , vbBinaryCompare)
  InputStr = Replace(InputStr, "'", vbNullString, 1, 1, vbBinaryCompare)

  OutputArr = Split(InputStr, " ", Compare:=vbBinaryCompare)

  Debug.Print Join(OutputArr, vbCrLf)
  Debug.Print TypeName(OutputArr)

  InputParser = OutputArr
  Debug.Print TypeName(InputParser)
End Function