代码之家  ›  专栏  ›  技术社区  ›  Serversta

VBA Excel用多列填充列表框

  •  6
  • Serversta  · 技术社区  · 7 年前

    对于一些人来说,这可能是一个廉价的问题,但我完全不知道如何填充我的列表框。

    form with listbox

    使用这一行,我可以填充列表框,如下所示:
    ListBox1.List = Sheets("Sheet1").Cells(1, 1).CurrentRegion.Value

    Dim rngName As Range
    Dim ws As Worksheet
    Dim i As Integer
    Set ws = Worksheets("Sheet1")
        For i = 1 To ws.Cells(ws.Rows.Count, 1).End(xlUp).Row Step 1
            If ws.Cells(i, 1).Value <> vbNullString Then Me.ListBox1.AddItem 
            ws.Cells(i, 1).Value
    Next i
    

    form with listbox 1 column output

    下面是我计划用于填充列表框的数据,并且是渐进的。只有列具有固定计数。
    Data

    有人请告诉我如何使用 FOR循环

    2 回复  |  直到 6 年前
        1
  •  13
  •   T.M.    7 年前

    方法

    1. 在数组中循环总是比在范围中循环要好-它是 快得多 .
    2. 使用 一个衬里 而不是像Siddharth Rout提出的那样重新确定预声明数组的尺寸并将其填充到额外的循环中(尽管这是一种很好的方法:-) 注: 下面的代码基于他在上述评论中引用的方法,只是为了证明差异。
    3. 填满 ListBox1.List 使用数组(方法相同,但方向相反)。

    密码

    Private Sub CommandButton1_Click()
    ' Purpose:  fill listbox with range values after clicking on CommandButton1
    '           (code could be applied to UserForm_Initialize(), too)
    ' Note:     based on @Siddharth-Rout 's proposal at https://stackoverflow.com/questions/10763310/how-to-populate-data-from-a-range-multiple-rows-and-columns-to-listbox-with-vb
    '           but creating a variant data field array directly from range in a one liner
    '           (instead of filling a redimensioned array with range values in a loop)
    Dim ws      As Worksheet
    Dim rng     As Range
    Dim MyArray                 ' variant, receives one based 2-dim data field array
    '~~> Change your sheetname here
    Set ws = Sheets("Sheet1")
    
    '~~> Set you relevant range here
    Set rng = ws.Range("A1:C" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row)
    
    With Me.ListBox1
        .Clear
        .ColumnHeads = False
        .ColumnCount = rng.Columns.Count
    
        '~~> create a one based 2-dim datafield array
         MyArray = rng
    
        '~~> fill listbox with array values
        .List = MyArray
    
        '~~> Set the widths of the column here. Ex: For 5 Columns
        '~~> Change as Applicable
        .ColumnWidths = "50;50;50"
        .TopIndex = 0
    End With
    End Sub
    

    其他提示

    • 数组方法的另一个优点-it 克服 仅的内置限制 10列 当使用 .AddItem 方法

    • 此外,请记住,列表框索引是 基于零的 例如,您可以通过以下方式获得第一个项目行(索引0)的电子邮件地址(第3列,索引2) ListBox1.List(0, 2) ,而数据场阵列自动成为基于一的2维阵列。

    • 您不受限制地使用 .List 方法从列表框中获取信息,可以使用 ListBox1.Column" or even create a new array out of it, which remains a 2-dim object, even if there is only ONE item (note: the 应用转置方法将仅具有一行的二维数组重新划分为一维数组)。

    • 最后一点:您可以通过 rng = ListBox1.List ,但请注意定义正确的范围。

        2
  •  4
  •   Xabier    7 年前

    Sub foo()
    Dim rngName As Range
    Dim ws As Worksheet
    Dim i As Integer
    Set ws = Worksheets("Sheet1")
    ListBox1.Clear
    ListBox1.columnCount = 3
    Dim LastRow As Long
    LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
        For i = 1 To LastRow
            If ws.Cells(i, 1).Value <> vbNullString Then ListBox1.AddItem ws.Cells(i, 1).Value
            If ws.Cells(i, 2).Value <> vbNullString Then ListBox1.List(i - 1, 1) = ws.Cells(i, 2).Value
            If ws.Cells(i, 3).Value <> vbNullString Then ListBox1.List(i - 1, 2) = ws.Cells(i, 3).Value
        Next i
    End Sub