Excel VBA - 如何将ListBox值插入字符串?

时间:2021-01-02 02:28:54

I have a ListBox in a userform that has items populated in it.

我在userform中有一个ListBox,其中填充了项目。

What I would like to do is add all of the items to a string seperated by a semicolon.

我想要做的是将所有项添加到由分号分隔的字符串中。

So far I have the following code:

到目前为止,我有以下代码:

For i = 0 To Me.lbSend.ListCount - 1
    Set strEmail = ws.Range("A:A").Find(What:="Me.lbSend.List(i)", LookAt:=xlWhole, MatchCase:=False, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
    strTo = strTo & ";" & strEmail.Offset(0,1).Value
Next i

However, it is not working and I can't quite put my finger on why.

但是,它不起作用,我不能完全理解为什么。

For each item in the list I need to find it in Column A in another worksheet and then add the value in the cell to the right of it to the string.

对于列表中的每个项目,我需要在另一个工作表的A列中找到它,然后将其右侧单元格中的值添加到字符串中。

1 个解决方案

#1


0  

For anyone else that requires that same answer:

对于其他需要相同答案的人:

@tigeravatar spotted the mistake and is correct with the following: (No quotes around Me.lbSend.List(i))

@tigeravatar发现了这个错误,并且正确如下:( Me.lbSend.List(i)周围没有引号)

For i = 0 To Me.lbSend.ListCount - 1
Set strEmail = ws.Range("A:A").Find(What:=Me.lbSend.List(i), LookAt:=xlWhole, MatchCase:=False, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
strTo = strTo & ";" & strEmail.Offset(0,1).Value
Next i

#1


0  

For anyone else that requires that same answer:

对于其他需要相同答案的人:

@tigeravatar spotted the mistake and is correct with the following: (No quotes around Me.lbSend.List(i))

@tigeravatar发现了这个错误,并且正确如下:( Me.lbSend.List(i)周围没有引号)

For i = 0 To Me.lbSend.ListCount - 1
Set strEmail = ws.Range("A:A").Find(What:=Me.lbSend.List(i), LookAt:=xlWhole, MatchCase:=False, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
strTo = strTo & ";" & strEmail.Offset(0,1).Value
Next i