且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从工作表上的列表框中获取值

更新时间:2023-12-01 10:35:46

不幸的是,对于MSForms列表框,循环遍历列表项并检查其Selected属性是唯一的方法.但是,这是另一种选择.我正在将选定的项目存储/删除到一个变量中,您可以在某个远程单元格中进行此操作并跟踪它:)

Unfortunately for MSForms list box looping through the list items and checking their Selected property is the only way. However, here is an alternative. I am storing/removing the selected item in a variable, you can do this in some remote cell and keep track of it :)

Dim StrSelection As String

Private Sub ListBox1_Change()
    If ListBox1.Selected(ListBox1.ListIndex) Then
        If StrSelection = "" Then
            StrSelection = ListBox1.List(ListBox1.ListIndex)
        Else
            StrSelection = StrSelection & "," & ListBox1.List(ListBox1.ListIndex)
        End If
    Else
        StrSelection = Replace(StrSelection, "," & ListBox1.List(ListBox1.ListIndex), "")
    End If
End Sub