Split


Description:
Implements the class StringCollection. This class can take a string in, split it up, and allow you to use For Each... to loop through the pieces of the string. I find this very useful, almost every project I work on ends up using it sooner or later.
 
Code:
Option Explicit

Private mCol As Collection

Public Sub SplitInto(sIn As String, Optional sDelim As String = ",")

    Dim sPart As String
    Dim iPos As Long
    Dim iLoc As Long

    iLoc = 0
    iPos = 1

    Do
        iLoc = InStr(iPos, sIn, sDelim)
        If iLoc > 0 Then
            sPart = Mid(sIn, iPos, iLoc - iPos)
            iPos = iLoc + Len(sDelim)
        Else
            sPart = Mid(sIn, iPos)
            iPos = 0
        End If
        mCol.Add sPart
    Loop Until iPos = 0

End Sub

Public Function Add(sValue As String) As String
    mCol.Add sValue
    Add = sValue
End Function

Public Property Get Item(nIndex As Long) As String
  Item = mCol(nIndex)
End Property

Public Property Get Count() As Long
    Count = mCol.Count
End Property

Public Sub Remove(nIndex As Long)
    mCol.Remove nIndex
End Sub

Public Property Get NewEnum() As IUnknown 'Procedure ID is -4
    Set NewEnum = mCol.[_NewEnum]
End Property

Private Sub Class_Initialize()
    Set mCol = New Collection
End Sub

Private Sub Class_Terminate()
    Set mCol = Nothing
End Sub
 
Sample Usage:
 
Dim sclLines As New StringCollection
sclLines.SplitInto Clipboard.GetText, vbNewLine

Dim vLine
For Each vLine In sclLines
    Debug.Print "[" & vLine & "]"
Next