简体   繁体   中英

Excel multi-select listbox VBA code working fine in Excel 2010 and 2016 but not in Office 365

The following VBA macro function has been working fine being developed in Office 2016 and being used in Office 2010 in a professional setting. Recently the Office 2010 infrastructure was upgraded to Office 365 and since then the following macro only updates one of the two multi-select boxes:

Sub Main_OK_CommandButton_Click()

Worksheets("Database").Activate
Worksheets("Database").Range("I50:J60").Clear

Dim i As Long
Dim j As Long
Dim m As Long
Dim n As Long

For i = 0 To Meeting_ListBox.ListCount - 1
    If Meeting_ListBox.Selected(i) Then
        j = j + 1
        Cells(j + 50, 9) = Meeting_ListBox.List(i)
    End If
Next

For m = 0 To Participant_ListBox.ListCount - 1
    If Participant_ListBox.Selected(m) Then
        n = n + 1
        Cells(n + 50, 10) = Participant_ListBox.List(m)
    End If
Next

End Sub

Under Excel 365 only the Meeting_ListBox selections will be transferred to the array regardless of the order in the macro or order of declared long values of i, j etc. The Participant_Listbox is ignored presumably as the selected options are not being identified. I know that the 2 listboxes have the same settings because I have copied the working one over to the other, renamed it, changed RowSource while keeping all other settings the same yet the issues remains. The RowSource lists are named ranges defined dynamically by their length using COUNTIF (at Database:$BH$40 in this example) with the following context:

=OFFSET(Database!$BH$42,0,0,Database!$BH$40,1)

There are 4 listboxes in this userform in total. Two of them are single select which update their outputs as soon as they are clicked. All along there has been some strange selection behaviour where previously selected multi-select listboxes appear to visually lose their selected status after one of the single select listboxes have been clicked. Despite this, it has always 'remembered' the multiple selection(s) despite appearing to have been de-selected. This 'forgetting' of the Participant_Listbox selections under Office 365 still occurs even if both multi-select listboxes have kept the visual selected status in the userform.

Based on an online search I have tried to 're-compile' under VBA debug in Excel 365 without success

I have discovered where the problem lies and it is related to the type of named range used in the rowsource of the ListBox. A simple range of cells as the rowsource works without issue. When the rowsource range is dynamically determined as described above it goes wrong. I ended up changing the way the rowsource ranges are dynamically updated. Still don't understand why the behavior changed but it is solved now.

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