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