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.
You can't debug.print an array!
Otherwise, the tweaked function provided by Jonners works fine.
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. I created SmartSplit which I use instead of Split. SmartSplit returns the original string in an array if no actual splitting occurs. This means that you don't need to test for an array after using 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.
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.