有一种选择列表框的方法(类似于用鼠标单击它吗?)

问题描述 投票:0回答:1

我正在为用户表单编写一些VBA代码。在左侧的列表框中选择这些值(LB_Participants)。然后按下“选择”,并将值复制到右侧的列表框(LB_Output)。然后,我希望VBA在LB_Output中分别遍历所有这些项目,并从另一个工作表中查找其他关联数据。我遇到的问题是某些值的值未选中。我用一个消息框检查它,并不时将其空白。然后,当然不能检索到任何关联的数据。在开始填写用户表单之前,如果我只单击一次LB_Output(即使未选择任何值),也不会出现此问题。许多人将使用用户窗体,所以我不想向他们解释他们必须先在列表框中单击才能继续。...我在做错什么吗?Blank Msgbox

Dim ListCount As Integer
Dim z As Integer

ListCount = UserForm2.LB_Output.ListCount

For z = 0 To ListCount - 1
UserForm2.LB_Output.Selected(z) = True
TextString = UserForm2.LB_Output.Value
MsgBox (TextString)

'Split Participants into seperate names and copy them to data sheet
    WArray() = Split(TextString, ";")
    For Counter = LBound(WArray) To UBound(WArray)
        Dim LRNames As Integer
        If IsEmpty(Sheets("Data").Range("A1")) = True Then
        LRNames = 0
        Else
        LRNames = Sheets("Data").Range("A" & Application.Rows.Count).End(xlUp).Row
        End If
        Strg = WArray(Counter)
        Sheets("Data").Cells(LRNames + 1, 1) = Trim(Strg)
    Next Counter
Next z
excel vba listbox listboxitem
1个回答
0
投票

不确定我是否理解,但是认为您想遍历LB_Output中的所有项目并对其进行处理,无论是否选中-所有选择均在另一个列表框中完成,并且那些项目已移至LB_Output。

For z = 0 to UserForm2.LB_Output.Listcount -1
   TextString = UserForm2.LB_Output.List(z)
   '// Do processing with this item
Next 
© www.soinside.com 2019 - 2024. All rights reserved.