简体   繁体   中英

How to multiselect more than one listbox simultaneously in excel VBA

Scenario

I have two listboxes named openItemList and serialNumber . serialNumber entries are unique numbers for every row of openItemList . It means every openItemList has a unique serialNumber . But openItemList can have duplicate values in different rows. User can understand only openItemList and they select this. 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.

Currently I am able to select both listboxes simultaneously because, I set multiselect property to single entry. 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. Due to this, I cannot search the respective rows based on serialNumber anymore. Anyone knows how to select both listbox simultaneously when change to MultiSelectExtended mode?

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

在此处输入图片说明

you have to:

  • use Change event instead of Click one

  • match both listboxes each element Selected property:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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