【问题标题】:VBA - Strange Combobox/Listbox behaviourVBA - 奇怪的组合框/列表框行为
【发布时间】:2017-10-26 10:07:50
【问题描述】:

以下代码是一个更大程序的一部分,用于收集一组跟踪移动的生产订单,这些订单需要在 SAP 中发布。 这个特殊的例程很简单,它只是收集订单,将它们放入一个数组中,然后将列表放入一个组合框中。

我遇到的问题是,如果我使用数组作为方法,组合框会按预期填充,没有任何错误,除了列表在那里,但不可见。如果单击该选项,将正确显示,否则。

或者,如果我用 AddItem 方法做同样的事情,事情是可见的。

我观察到列表框的相同行为,其中的项目将填充,但不可见,如果我尝试使用数组,但使用 addItem 方法可见。我已经用这两种方法测试了代码,重置了 Excel 和我的电脑,并试图弄清楚它是否是我不小心点击的某个属性,但没有任何东西跳出来。

以下代码供参考

提前谢谢你。

   Private Sub POs_for_SAP()

    'this routine is going to create the list of POs and populate the combo box with them

    Dim lstcl As Variant, cell As Range, arr_po() As Variant, x As Integer

    With ThisWorkbook.Sheets("Staging")

    lstcl = .Range("B10000").End(xlUp).row

    'UserForm12.cboPOSAP.Clear

    For Each cell In .Range("B4:B" & lstcl)

        If Not IsEmpty(cell) And IsEmpty(cell.Offset(0, 7)) Then

            'UserForm12.cboPOSAP.AddItem cell

            ReDim Preserve arr_po(x)
            Set arr_po(x) = cell
            x = x + 1

        End If

    Next

    End With

    With UserForm12.cboPOSAP

            .Clear
            .List = arr_po()
            .Style = fmStyleDropDownList

        End With

    UserForm12.Show

    End Sub

【问题讨论】:

  • 每当我遇到这种古怪的行为时,我都会使用VBA Cleaner & Compressor for Excel, PowerPoint & Word 清理工作簿。经过反复使用和代码修改,工作簿变得臃肿,奇怪的事情发生了。
  • @ThomasInzina,与我对这个文件所做的一样多的编码和测试,很可能就是这样......谢谢你的推荐!
  • 组合框有一个列宽设置。它可能为零
  • @jsotola,改成1,但没区别
  • 设置 arr_po(x) = cell ,不需要“设置”。只有 arr_po(x) = 单元格

标签: vba excel


【解决方案1】:

在数组中不需要set语句。

Private Sub POs_for_SAP()

    'this routine is going to create the list of POs and populate the combo box with them

    Dim lstcl As Variant, cell As Range, arr_po() As Variant, x As Integer

    With ThisWorkbook.Sheets("Staging")

    lstcl = .Range("B10000").End(xlUp).Row

    'UserForm12.cboPOSAP.Clear

    For Each cell In .Range("B4:B" & lstcl)

        If Not IsEmpty(cell) And IsEmpty(cell.Offset(0, 7)) Then

            'UserForm12.cboPOSAP.AddItem cell

            ReDim Preserve arr_po(x)
            arr_po(x) = cell '<~~~no need set
            x = x + 1

        End If

    Next

    End With

    With UserForm12.cboPOSAP

            .Clear
            .List = arr_po()
            .Style = fmStyleDropDownList

        End With

    UserForm12.Show

    End Sub

【讨论】:

    猜你喜欢
    • 2010-12-23
    • 1970-01-01
    • 2015-10-12
    • 2015-01-14
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 2012-12-25
    • 1970-01-01
    相关资源
    最近更新 更多