How to turn Listbox to Text for Excel VBA

后端 未结 1 1071
庸人自扰
庸人自扰 2021-01-28 18:44

I am trying to automatize an e-mail, but I am having a problem when I try to send lines from listbox; I have tried a few different ways none that were even close to working. In

相关标签:
1条回答
  • 2021-01-28 19:43

    If you have allowed the MultiSelect property for the listbox to True, try this...

    Dim listboxarr()
    Dim i As Long, j As Long
    
    'Assuming the name of your ListBox is ListBox1. If not, change it in the following code.
    
    With Me.ListBox1
        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                j = j + 1
                ReDim Preserve listboxarr(1 To j)
                listboxarr(j) = .List(i)
            End If
        Next i
    End With
    

    Edited Code:

    Dim listboxarr()
    Dim i As Long, j As Long
    Dim found As Boolean
    
    'Assuming the name of your ListBox is ListBox1. If not, change it in the following code.
    
    With Me.ListBox1
        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                found = True
                j = j + 1
                ReDim Preserve listboxarr(1 To j)
                listboxarr(j) = .List(i)
            End If
        Next i
    End With
    

    And then you can use it like below...

    .body = IIf(found, Join(listboxarr, ", "), "No item selected")
    
    0 讨论(0)
提交回复
热议问题