Search This Blog

Monday, March 22, 2010

Parse items to list box

Simple little sub I wrote to parse multiple items from a text box or string and add them to a listbox.

I thought you guys could get some use out of it... So here goes:

Somewhere in your form you need this code...


Public Sub l4nParse(ByVal strSource, strStart, strEnd As String, lSt As ListBox)
Dim lonPos, lonEnd As Long
Dim strParsed As String

'Make sure that the start of what we want to parse is really there.
lonPos = InStr(1, strSource, strStart, vbTextCompare)
'Do the following loop for as long as the starting string is in strSource
Do While lonPos > 0
lonPos = lonPos + Len(strStart)
lonEnd = InStr(lonPos, strSource, strEnd, vbTextCompare)
If lonEnd > 0 Then
strParsed = Mid$(strSource, lonPos, lonEnd - lonPos)
strParsed = Replace(strParsed, vbCrLf, vbNullString)
lSt.AddItem (strParsed)
lonPos = InStr(lonEnd, strSource, strStart, vbTextCompare)
Else
lonPos = lonEnd
End If
Loop
End Sub

And thats it. Now all you need to do is specify a source string, the string before what you want parsed, and the string after what you want parsed.

You would call l4nParse like this:


l4nparse strSource, strStart, strEnd, lstbox

And thats it.

No comments:

Post a Comment