[英]VBA function to return array from string input
我编写了一个函数来执行一些基本的字符串解析,但是由于我目前不知道的原因,我的编译器不断将我的代码标记为类型不匹配错误。 该函数采用字符串,用空格替换某些字符,然后使用拆分将输出转换为数组。 然后将该函数设置为输出数组。 运行时,函数完成其过程,但是编译器随后在调用模块中引发类型不匹配错误。 代码如下,在此先感谢您的帮助。
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
问题是当您使用Debug.Print尝试查看该函数的结果时。
您不能debug.print数组!
否则,Jonners提供的调整功能可以正常工作。
如果需要查看返回的数组中的内容,则需要在创建数组后放置一个断点,然后使用“本地”窗口对其进行检查。
另外,如果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
我希望这有帮助。
缺口
首先,您不需要围绕“替换”进行条件测试-如果输入字符串不包含目标字符,则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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.