简体   繁体   中英

How to turn Listbox to Text for Excel VBA

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 addition, I don't know how to use the column. I am currrently tryying to get it to work via

Dim listboxarr()
Dim i As Integer

For i = 1 To 500
'    v this is a listbox
     With selecteditems
         listboxarr(1) = .List(i, 1)
     End With
Next i

This code throws me:

Subscription out of Range

This is the code for the email:

Private Sub addcb_Click()
Dim iCtr As Long

For iCtr = 0 To Me.allitems.ListCount - 1
    If Me.allitems.Selected(iCtr) = True Then
        Me.selecteditems.AddItem Me.allitems.List(iCtr)
    End If
Next iCtr

For iCtr = Me.allitems.ListCount - 1 To 0 Step -1
    If Me.allitems.Selected(iCtr) = True Then
        Me.allitems.RemoveItem iCtr
    End If
Next iCtr
End Sub


Private Sub removecb_Click()
Dim iCtr As Long

For iCtr = 0 To Me.selecteditems.ListCount - 1
    If Me.selecteditems.Selected(iCtr) = True Then
        Me.allitems.AddItem Me.selecteditems.List(iCtr)
    End If
Next iCtr

For iCtr = Me.selecteditems.ListCount - 1 To 0 Step -1
        If Me.selecteditems.Selected(iCtr) = True Then
            Me.selecteditems.RemoveItem iCtr
        End If
Next iCtr
End Sub

Private Sub CommandButton1_Click()

Dim listboxarr()
Dim i As Integer

For i = 1 To 500
'    v this is a listbox
     With selecteditems
         listboxarr(1) = .List(i, 1)
     End With
Next i

Dim OutApp As Object
Dim OutMail As Object

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
    .to = "Someone"
    .CC = "Someone else"
    .BCC = ""
    .Subject = "Something"
    .Body = listboxarr(1) 
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing

Private Sub UserForm_Initialize()

Dim itemsheet As Worksheet
Set itemsheet = Application.ActiveWorkbook.Sheets(6)

For Each itemname In itemsheet.Range("C2:C3285")
    With Me.allitems
       .AddItem itemname.Value
    End With
Next itemname

End Sub

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM