21 | 08 | 2017

Split String

A string can be a marriage of other strings.  For instance telephone numbers can be divided by a - symbol. With the function below you can split functions that are hold together by a symbol. Just enter the string and the symbol and the function will return all values that are divided by that symbol.

Function Split(ByVal strInput As String, Optional SplitSymbol As String = ";")

'*** Code from VisibleVisual.com ***

'This Function divides a string and returns all substrings that are hold together by the SplitSymbol
'The default Split Symbol is ; but this can be overruled by every symbol you like

Dim outarray() As Variant
Dim arrsize As Integer
While InStr(strInput, SplitSymbol) > 0
ReDim Preserve outarray(0 To arrsize) As Variant
outarray(arrsize) = Left(strInput, InStr(strInput, SplitSymbol) - 1)
strInput = Mid(strInput, InStr(strInput, SplitSymbol) + Len(SplitSymbol))
arrsize = arrsize + 1


ReDim Preserve outarray(0 To arrsize) As Variant
outarray(arrsize) = strInput

'Loop through the array
For i = 0 To UBound(outarray)
Split = Split & outarray(i) & Chr$(13) & Chr$(10)
Next i

End Function

Sub TestSplitFunction()

'Some examples of how to use the Split Function
MsgBox Split("022-645-323-4354-23232323-21", "-")
MsgBox Split("DIVIDED BY SPACES", " ")

End Sub

Sign up now and upload your code to the website.

Help us to continue.....
Articles View Hits
Latest Articles