0

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

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
4

2 回答 2

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

我希望这有帮助。

缺口

于 2013-04-16T23:39:08.713 回答
0

首先,您不需要围绕“替换”进行条件测试 - 如果输入字符串不包含目标字符,Replace则不会执行任何操作。

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

调整功能:

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
于 2013-04-15T14:41:41.403 回答