简体   繁体   English

Excel 多选列表框 VBA 代码在 Excel 2010 和 2016 中运行良好,但在 Office 365 中运行不正常

[英]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.以下 VBA 宏 function 在 Office 2016 中开发并在专业环境中用于 Office 2010 时运行良好。 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:最近 Office 2010 基础架构升级到 Office 365,从那时起,以下宏仅更新两个多选框之一:

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.在 Excel 365 下,只有 Meeting_ListBox 选择将被传输到数组,而不管宏中的顺序或 i、j 等声明的长值的顺序。 Participant_Listbox 可能被忽略,因为所选选项未被识别。 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.我知道这 2 个列表框具有相同的设置,因为我已将工作中的一个复制到另一个,重命名它,更改 RowSource,同时保持所有其他设置相同,但问题仍然存在。 The RowSource lists are named ranges defined dynamically by their length using COUNTIF (at Database:$BH$40 in this example) with the following context: RowSource 列表是使用 COUNTIF(在本例中为数据库:$BH$40)根据其长度动态定义的命名范围,上下文如下:

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

There are 4 listboxes in this userform in total.此用户窗体中总共有 4 个列表框。 Two of them are single select which update their outputs as soon as they are clicked.其中两个是单个 select,一旦单击它们就会更新它们的输出。 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.一直以来都存在一些奇怪的选择行为,即在单击单个 select 列表框之一后,先前选择的多选列表框似乎在视觉上失去了其选中状态。 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.即使两个多选列表框都在用户窗体中保持可视选择状态,Office 365 下 Participant_Listbox 选择的这种“遗忘”仍然会发生。

Based on an online search I have tried to 're-compile' under VBA debug in Excel 365 without success基于在线搜索,我尝试在 Excel 365 中的 VBA 调试下“重新编译”,但没有成功

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.仍然不明白为什么行为会改变,但现在已经解决了。

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

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