An Enhanced Replacement For VBA's Split Function
This page describes code that enhances the VBA Split function.
VBA provides a very useful function called
Split that
transforms a delimited string into an array. The
SplitEx function
builds on
Split to provide considerably more flexibility in
how a string should be split. The function declaration of
SplitEx is
Function SplitEx(InString As String, _
Delimiter As String, _
Optional GroupChar As String = vbNullString, _
Optional IgnoreConsecutive Delimiters As Boolean = False, _
Optional Escape As String = vbNullString, _
Optional RemoveEscape As Boolean = True, _
Optional DeleteGroupCharacters As Boolean = False) As String()
If all optional parameters are omitted, SplitEx performs
just like VBA's Split function. The options provide
SplitEx
InStringg is the initial string to be split.
Delimiter is the character used to split InString.
GroupChar specifies a character which prevents splitting on a delimiter that
falls between two instances of GroupChar. For example, if
InString is abc "def ghi" xyz and
GroupChar is a double quote, Chr(34), SplitEx will
keep "def ghi"GroupCharr characters.
IgnoreConsecutiveDelimiters indicates that SplitEx is
to ignore two consecutive delimiters with no text between them For example, if Delimiter
is | and InString is abc|def||ghi, the
normal results is four elements: abc , def ,
(empty) , and ghi. If IgnoreConsecutiveDelimiters
is True the result does not include the (empty) element between the two
consecutive delimiters.
EscapeSplitExx to ignore that delimiter. For example, if
InString is abc|def\|ghi|xyz ,the delimiter
| between def and ghi is not
treated as a delimiter and the text def\|ghi is the complete second element
of the result.
RemoveEscape indicates whether SplitEx should remove the
escape character from the output.
DeleteGroupCharacters indicates whether to delete GroupChar from
the final results.
The following is the complete code of the SplitEx function.
Function SplitEx(InString As String, _
Delimiter As String, _
Optional GroupChar As String = vbNullString, _
Optional IgnoreConsecutiveDelimiters As Boolean = False, _
Optional Escape As String = vbNullString, _
Optional RemoveEscape As Boolean = True, _
Optional DeleteGroupCharacters As Boolean = False) As String()
Dim InGroup As Boolean
Dim Arr() As String
Dim N As Long
Dim InGroupReplace As String
Dim S As String
Dim Done As Boolean
Dim M As Long
Dim EscapeReplace As String
If InString = vbNullString Then
SplitEx = Arr
Exit Function
End If
If Len(Delimiter) = 0 Then
ReDim Arr(0 To 0)
Arr(0) = InString
Exit Function
End If
S = InString
N = 1
Done = False
Do Until Done
If StrComp(Chr(N), Delimiter, vbBinaryCompare) <> 0 Then
M = InStr(1, S, Chr(N), vbBinaryCompare)
If M = 0 Then
InGroupReplace = Chr(N)
Done = True
End If
End If
N = N + 1
Loop
InGroupReplace = Chr(N)
N = N + 1
Done = False
If Escape <> vbNullString Then
Do Until Done
If StrComp(Chr(N), Escape, vbTextCompare) <> 0 Then
M = InStr(1, S, Chr(N), vbBinaryCompare)
If M = 0 Then
EscapeReplace = Chr(N)
Done = True
End If
End If
N = N + 1
Loop
End If
If Escape <> vbNullString Then
S = Replace(S, Escape & Delimiter, EscapeReplace)
End If
If IgnoreConsecutiveDelimiters = True Then
N = InStr(1, S, Delimiter & Delimiter, vbBinaryCompare)
Do Until N = 0
S = Replace(S, Delimiter & Delimiter, Delimiter)
N = InStr(1, S, Delimiter & Delimiter, vbBinaryCompare)
Loop
End If
If Len(GroupChar) > 0 Then
For N = 1 To Len(S)
If Mid(S, N, Len(GroupChar)) = GroupChar Then
InGroup = Not InGroup
End If
If Mid(S, N, 1) = Delimiter Then
If InGroup Then
Mid(S, N, 1) = InGroupReplace
End If
End If
Next N
End If
Arr = Split(S, Delimiter)
For N = LBound(Arr) To UBound(Arr)
Arr(N) = Replace(Arr(N), InGroupReplace, Delimiter)
If DeleteGroupCharacters = True Then
Arr(N) = Replace(Arr(N), GroupChar, vbNullString)
End If
If EscapeReplace <> vbNullString Then
If RemoveEscape = True Then
Arr(N) = Replace(Arr(N), EscapeReplace, Delimiter)
Else
Arr(N) = Replace(Arr(N), EscapeReplace, Escape & Delimiter)
End If
End If
Next N
SplitEx = Arr
End Function
|
This page last updated: 10-July-2010. |