Search code examples
vbams-worduserform

How to delimit listbox selections by comma?


I have a userform with a list of selections for three headings in a table. Each heading will have at least one selection. I'm trying to separate each selection with a comma and end the list with a period.

The table is populated on button click.

Private Sub CommandButton6_Click()
    Dim tableSequence As Table
    Set tableSequence = ActiveDocument.Tables(1)
    Dim NewRow As Row
    Set NewRow = tableSequence.Rows.Add
    Dim MyString2 As String
    Dim MyString5 As String
    Dim v As Variant
    Dim t As String
    Dim r As String
    Dim i As Long
    Dim L As Long
    Dim var
    Dim var1
    Dim MyString3 As String
    Dim MyString4 As String
    Dim var2
    Dim var3
    Dim p As String
    Dim M As Long
    Dim q As String
    Dim Y As Long
    For var3 = 0 To ListBox7.ListCount - 1
        If ListBox7.Selected(var3) = True Then
            MyString5 = MyString5 & ListBox7.List(var3)
            v = Split(MyString5, ",")
            p = ""
            For M = LBound(v) To UBound(v)
                p = p + v(M)
                If M Mod 3 = 2 Then
                    p = p + vbCr
                Else
                    p = p + ","
                End If
            Next M
            p = Left(p, Len(p) - 1)
            Debug.Print p
        End If
    Next
      
    NewRow.Cells(2).Range.Text = TextBox8.Text
   
    NewRow.Cells(3).Range.Text = TextBox9.Text
    
    NewRow.Cells(4).Range.Text = MyString2
    
    NewRow.Cells(5).Range.Text = "Engineering: " & MyString3 & "." _
      & vbCrLf & "Administrative: " _
      & MyString4 & "." & vbCrLf & "PPE: " & MyString5 & "."
    NewRow.Cells(5).Range.Bold = False
    NewRow.Cells(5).Range.Underline = False
    
    NewRow.Cells(6).Range.Text = ComboBox1.Text
    
    NewRow.Cells(7).Range.Text = ComboBox2.Text
    
    NewRow.Cells(8).Range.Text = ComboBox3.Text
    Dim keywordArr As Variant
    keywordArr = Array("Engineering:", "Administrative:", "PPE:")

    Dim keyword As Variant
    Dim myRange As Variant
    Dim startPos As Integer
    Dim endPos As Integer
    Dim length As Integer
    Dim i1 As Integer
    i1 = 1

    For Each keyword In keywordArr

        Do While InStr(1, myRange, keyword) = 0
            Set myRange = NewRow.Cells(5).Range.Paragraphs(i1).Range
            i1 = i1 + 1
        Loop
        startPos = InStr(1, myRange, keyword)
        startPos = myRange.Characters(startPos).Start
        length = Len(keyword)
        endPos = startPos + length
    
        Set myRange = ActiveDocument.Range(startPos, endPos)
        With myRange.Font
            .Bold = True
            .Underline = True
        End With
    Next keyword

End Sub

I believe that the portion of code from For var3 to the second end if should provide my comma delimiter.

The attached image shows what I'm getting on the left vs. what I'm trying to get on the right.
Actual Vs Target


Solution

  • You need to add the commas as you build the string.

       For var3 = 0 To ListBox7.ListCount - 1
          If ListBox7.Selected(var3) = True Then
             If MyString5 = vbNullString Then
                MyString5 = ListBox7.List(var3)
             Else
                MyString5 = MyString5 & ", " & ListBox7.List(var3)
             End If
          End If
       Next