列表框不显示使用Listbox.List方法填充的值

时间:2018-11-20 07:47:20

标签: excel vba excel-vba listbox

运行Scaffold事件后,列表框中将不会显示任何内容,如下所示:

enter image description here

基于下面的excel表,应该有11列填充列表框:

enter image description here

代码运行了:

Userform_Initialize()

列表框的属性:

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以填充每个列表行,然后向其中添加列:

Option Explicit

Private Sub UserForm_Initialize()
    Dim tblFoilProfile As ListObject
    Set tblFoilProfile = ThisWorkbook.Worksheets("Foil Profile").ListObjects("tblFoilProfile")

    Dim i As Long

    lbxFoilInfoDisplay.Clear

    Dim iListRow As Range
    For Each iListRow In tblFoilProfile.DataBodyRange.SpecialCells(xlCellTypeVisible).Rows
        With Me.lbxFoilInfoDisplay
            .AddItem iListRow.Cells(1, 1).Value 'add first value (column 1)

            Dim iCol As Long
            For iCol = 2 To iListRow.Columns.Count 'add all other columns to that row
                .list(i, iCol) = iListRow.Cells(1, iCol).Value '.Value for unformatted value or .Text to show it in the same format as in the cell
            Next iCol
            i = i + 1
        End With
    Next iListRow
End Sub

请注意,这里是nice guide how to work with list objects