Userform列表框填充范围

时间:2015-11-26 13:57:07

标签: excel vba excel-vba controls userform

有什么方法可以按用户范围的单元格填充列表框或其他功能?

我想将每个选中的列放入1个列表框中,如下所示:

here

例如A2:U100没有为每列创建新的列表框?

现在我这样做:

ListBox1.List = Application.Worksheets("Můj_Ranking").Range("B2:B" & lastRw).Value
ListBox2.List = Application.Worksheets("Můj_Ranking").Range("C2:C" & lastRw).Value
ListBox3.List = Application.Worksheets("Můj_Ranking").Range("D2:D" & lastRw).Value
ListBox4.List = Application.Worksheets("Můj_Ranking").Range("E2:E" & lastRw).Value
ListBox5.List = Application.Worksheets("Můj_Ranking").Range("F2:F" & lastRw).Value
ListBox6.List = Application.Worksheets("Můj_Ranking").Range("G2:G" & lastRw).Value
ListBox7.List = Application.Worksheets("Můj_Ranking").Range("H2:H" & lastRw).Value
ListBox8.List = Application.Worksheets("Můj_Ranking").Range("I2:I" & lastRw).Value
ListBox9.List = Application.Worksheets("Můj_Ranking").Range("J2:J" & lastRw).Value
ListBox10.List = Application.Worksheets("Můj_Ranking").Range("K2:K" & lastRw).Value
ListBox11.List = Application.Worksheets("Můj_Ranking").Range("L2:L" & lastRw).Value

3 个答案:

答案 0 :(得分:1)

所以你确实想要一个包含多列的ListBox,这样的东西应该会有所帮助:

With ListBox1
    .ColumnCount = 11
    .ColumnWidths = "50;50;50;50;50;50;50;50;50;50;50"
    .ColumnHeads = False
    .RowSource = "=Můj_Ranking!B2:L" & LastRw
    .MultiSelect = fmMultiSelectMulti
End With

或如何循环控制:

For i = 1 To 11
    With Application.Worksheets("Můj_Ranking")
         Controls("ListBox" & i).List = .Range(ColLet(i) & "2:" & ColLet(i) & lastRw).Value
    End With
Next i

对于大多数控件,您还拥有.RowSource属性! ;)

获取列的字母的函数:

Public Function ColLet(x As Integer) As String
With ActiveSheet.Columns(x)
    ColLet = Left(.Address(False, False), InStr(.Address(False, False), ":") - 1)
End With
End Function

答案 1 :(得分:0)

尚未对Listbox进行测试,但这里是我如何使用记录集的结果填充Combobox

 Function Fill_Combobox(ByRef cbo As ComboBox, ByVal rs As ADODB.Recordset, ByVal colWidth As String)
    Dim aryColumnWidth() As String
    Dim i As Integer

    aryColumnWidth = Split(colWidth, ";")
    cbo.Clear
    cbo.ColumnCount = UBound(aryColumnWidth) + 1
    cbo.ColumnHeads = False
    cbo.ColumnWidths = colWidth

    Do Until rs.EOF
        With cbo
            .AddItem
            For i = 0 To UBound(aryColumnWidth)
                .List(.ListCount - 1, i) = rs.Fields(i)
            Next
        End With
        rs.MoveNext
    Loop
End Function 

它应该与列表框类似。调用AddItem方法将新条目添加到列表框,然后通过访问List元素

来填充它

答案 2 :(得分:0)

假设我正确地阅读了您的问题,这应该添加一行,其中包含您在单个ListBox中的列数。

my_file_path = '/home/ro/A_Python_Scripts/flask-auto/myDirectory/scarlett Johanson/1448543562.17.jpg/'

请记住在您的用户表单上编辑ListBox控件的列计数属性它不会起作用:)