简体   繁体   English

如何在Excel VBA中同时多选一个以上的列表框

[英]How to multiselect more than one listbox simultaneously in excel VBA

Scenario 脚本

I have two listboxes named openItemList and serialNumber . 我有两个名为openItemListserialNumber列表框。 serialNumber entries are unique numbers for every row of openItemList . serialNumber条目是openItemList每行的唯一编号。 It means every openItemList has a unique serialNumber . 这意味着每个openItemList都有一个唯一的serialNumber But openItemList can have duplicate values in different rows. 但是openItemList可以在不同的行中具有重复的值。 User can understand only openItemList and they select this. 用户只能理解openItemList并选择它。 Since respective serialNumber also select based on openItemList selection, my program is finding related row data by using serialNumber as search word criteria and update the correct row accordingly. 由于相应的serialNumber也基于openItemList选择进行选择,因此我的程序正在使用serialNumber作为搜索词标准来查找相关的行数据,并相应地更新正确的行。

Currently I am able to select both listboxes simultaneously because, I set multiselect property to single entry. 目前,我能够同时选择两个列表框,因为我将multiselect属性设置为单个条目。 I use following code to do so 我使用以下代码来做到这一点

Private Sub openItemList_Click()
    serialNumber.ListIndex = openItemList.ListIndex
End Sub

Private Sub openItemList_Scroll()
    serialNumber.TopIndex = openItemList.TopIndex
End Sub

Private Sub serialNumber_Click()
    openItemList.ListIndex = serialNumber.ListIndex
End Sub

Private Sub serialNumber_Scroll()
    openItemList.TopIndex = serialNumber.TopIndex
End Sub

Current Problem 当前问题

Now I want to let user to select multiple rows in the listbox so that user can update multiple rows at once. 现在,我想让用户在列表框中选择多个行,以便用户可以一次更新多个行。 The moment I changed property of the two listbox to MultiSelectExtended , the selection dependency is no more working. 当我将两个列表框的属性更改为MultiSelectExtended ,选择依赖项不再起作用。 Due to this, I cannot search the respective rows based on serialNumber anymore. 因此,我无法再基于serialNumber搜索相应的行。 Anyone knows how to select both listbox simultaneously when change to MultiSelectExtended mode? 有人知道在更改为MultiSelectExtended模式时如何同时选择两个列表框吗?

Currently this is how the two listboxes behave. 当前,这就是两个列表框的行为。 The moment I choose one listbox, the respective value of the other listbox highlight as well. 当我选择一个列表框时,另一个列表框的相应值也会突出显示。

在此处输入图片说明

The moment I change to Multiselect, it is acting as below without any simultaneous selection 我更改为Multiselect的那一刻,它的行为如下,没有同时进行选择

在此处输入图片说明

you have to: 你必须:

  • use Change event instead of Click one 使用Change事件而不是Click一个

  • match both listboxes each element Selected property: 匹配两个列表框,每个元素的Selected属性:

as follows: 如下:

Private Sub openItemList_Change()
    Dim i As Long
    With openItemList
        For i = 0 To .ListCount - 1
            serialNumber.Selected(i) = .Selected(i)
        Next
    End With
End Sub

also, change openItemList_Scroll() to openItemList_MouseDown() to pair listboxes visible elements 同样,将openItemList_Scroll()更改为openItemList_MouseDown()以配对列表框可见元素

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

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