简体   繁体   English

如何将列表框转换为Excel VBA的文本

[英]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... 如果您已将列表框的MultiSelect属性设置为True,请尝试以下操作...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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