【问题标题】:Is there a way to select a listbox (similar to clicking on it with mouse?)有没有办法选择一个列表框(类似于用鼠标单击它?)
【发布时间】:2020-03-09 14:07:29
【问题描述】:

我正在为用户表单编写一些 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

【问题讨论】:

  • 不在机器上,所以不能看但是... SetFocus?
  • 能不能不用.List来获取选中的值?

标签: excel vba listbox listboxitem


【解决方案1】:

不确定我是否理解,但认为您想遍历 LB_Output 中的所有项目并处理它们,无论是否选中 - 所有选择都在另一个列表框中完成,并且这些项目移动到 LB_Output。

这不会显式选择每个项目,只是从中获取数据。

For z = 0 to UserForm2.LB_Output.Listcount -1
   ' If you want to select the item to show 'progress' through the list,
   ' uncomment ...
   ' LB_Output.listindex = z
   ' The next line will still work as is
   TextString = UserForm2.LB_Output.List(z)
   '// Do processing with this item
Next 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    • 2017-04-08
    • 2015-12-18
    • 1970-01-01
    • 2021-08-28
    • 2010-09-08
    相关资源
    最近更新 更多