Update ListBox from comma-separated text

Updates a comma-separated text in a TextBox, adding newly selected values in a ListBox or removing deselected ones, leaving everything not in the ListBox untouched.

Private Sub UpdateMe(List As ListBox, Items As TextBox)
  Dim MergedData As String
  Dim iArray As Variant
  Dim nArray As Variant
  Dim Found As Boolean
 
  If (Not IsNull(Items.Value)) Then
    MergedData = Items.Value
    iArray = Split(MergedData, ",", -1, vbTextCompare)
  Else
    iArray = Array()
  End If
  nArray = Array("PRE")
 
  For intI = 0 To List.ListCount
    If List.Selected(intI) Then
      nArray = Split(Join(nArray, ",") & "," & List.ItemData(intI), ",", -1, vbTextCompare)
    End If
  Next intI
  For Each curItem In iArray
    curItem = Trim(curItem)
    Found = False
    For intI = 0 To List.ListCount
      If List.ItemData(intI) = curItem Then
        Found = True
      End If
    Next intI
    If Not Found Then nArray = Split(Join(nArray, ",") & "," & curItem, ",", -1, vbTextCompare)
  Next curItem
 
  MergedData = Join(nArray, ", ")
  MergedData = Replace(MergedData, "PRE, ", "")
  MergedData = Replace(MergedData, "PRE", "")
  Items.Value = MergedData
End Sub

 
snippets/vba/update-listbox-from-csv.txt · Last modified: 2008-07-15 23:19.51 by mbirth
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki Contents powered by Club-Mate Contents powered by BassDrive.com Labelled with ICRA