VBA - 奇怪的组合框/列表框行为

时间:2017-10-26 01:24:51

标签: excel vba excel-vba

以下代码是更大程序的一部分,用于收集跟踪需要在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

1 个答案:

答案 0 :(得分:0)

在数组中不需要设置语句。

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
相关问题