简体   繁体   English

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

[英]VBA function to return array from string input

I wrote a function to do some basic string parsing but for reasons currently unknown to me, my compiler keeps flagging my code for a type mismatch error. 我编写了一个函数来执行一些基本的字符串解析,但是由于我目前不知道的原因,我的编译器不断将我的代码标记为类型不匹配错误。 The function takes a string, replaces some characters with a space and then uses a split to turn the output into an array. 该函数采用字符串,用空格替换某些字符,然后使用拆分将输出转换为数组。 The function is then set to the output array. 然后将该函数设置为输出数组。 When run, the function complete's its process but the compiler then throws the type mismatch error in the calling module. 运行时,函数完成其过程,但是编译器随后在调用模块中引发类型不匹配错误。 Code is below, thank you in advance for your assistance. 代码如下,在此先感谢您的帮助。

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

The problem is when you use Debug.Print to try to see the results of the function. 问题是当您使用Debug.Print尝试查看该函数的结果时。

You can't debug.print an array! 您不能debug.print数组!

Otherwise, the tweaked function provided by Jonners works fine. 否则,Jonners提供的调整功能可以正常工作。

If you need to see what's in the returned array, you will need to put a breakpoint after the array has been created, and then use the Locals window to examine it. 如果需要查看返回的数组中的内容,则需要在创建数组后放置一个断点,然后使用“本地”窗口对其进行检查。

Also, Split annoyingly returns NULL if it doesn't have anything to work with. 另外,如果Split没有任何可使用的内容,则恼人地返回NULL。 I created SmartSplit which I use instead of Split. 我创建了SmartSplit而不是Split。 SmartSplit returns the original string in an array if no actual splitting occurs. 如果没有实际拆分,SmartSplit将返回数组中的原始字符串。 This means that you don't need to test for an array after using Split. 这意味着您在使用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

I hope this helps. 我希望这有帮助。

Nick 缺口

Firstly, you don't need the conditional test around the 'replace' - if the input string doesn't contain the target character, Replace won't do anything. 首先,您不需要围绕“替换”进行条件测试-如果输入字符串不包含目标字符,则Replace将不执行任何操作。

Secondly, your output array is only populated if you do actually replace something; 其次,仅当您确实替换某些内容时,才会填充输出数组; in the situation where the string does not contain anything that needs replacing, your function returns a 'null' array. 在字符串不包含任何需要替换的内容的情况下,您的函数将返回“ null”数组。

Tweaked function: 调整功能:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM