【问题标题】:VBA Excel Populate ListBox with multiple columnsVBA Excel用多列填充列表框
【发布时间】:2018-05-11 17:34:14
【问题描述】:

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

使用这一行,我可以填充列表框,如下所示:
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

以下是我计划用来填充列表框的数据,并且是渐进式的。只有列具有修复计数。


请有人告诉我如何使用 FOR LOOP 填充一个适应多列和多行的列表框,如我上面的代码所示。任何帮助表示赞赏。谢谢。

【问题讨论】:

  • 你见过THIS
  • 非连续范围见THIS
  • @SiddharthRout 这些行包括声明特定范围。是否可以使用所有非空白单元格的范围?
  • 你为什么想要循环播放?不这样做要快得多。
  • 不能使用动态命名范围吗?

标签: vba excel listbox userform


【解决方案1】:

这个怎么样:

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

【讨论】:

  • 感谢您不厌其烦地帮助我。我很感激!
【解决方案2】:

方法

  1. 循环遍历数组总是比遍历范围要好 - 它要快得多
  2. 使用 one liner 创建变体数据字段数组甚至比 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

其他提示

  • 数组方法的另一个优点 - 它克服了使用.AddItem 方法时只有10 列 的内置限制。

  • 此外,请记住,列表框索引是从零开始的,因此例如,您将获得第一个项目行(索引 0)的电子邮件地址(第 3 列,索引 2) ) 通过ListBox1.List(0, 2),而数据字段数组自动成为一个基于 1 的二维数组。

  • 您不限于使用 .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: theApplication.Transpose 方法反转行 - 列顺序将重新调整二维数组1-dim 数组只有一行)。

  • 最后一点:您可以通过rng = ListBox1.List 轻松地将整个列表框再次转储到 Excel 工作表,但请注意定义正确的范围。

【讨论】:

  • 感谢您为我提供的积极学习环境。我知道你给我的方法更好!
  • 很高兴我能帮上忙,在我的编辑中看到一些额外的提示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-05
  • 1970-01-01
相关资源
最近更新 更多