簡體   English   中英

VBA函數從字符串輸入返回數組

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM