在列表框中显示不连续的单元格范围

时间:2013-06-12 08:43:12

标签: vba

我正在尝试在名为“Departments”(A1:A10,C1:C10,E1:E10)的表格中显示一个不连续的单元格区域到多列列表框。

我在这里找到了一个例子,但它只显示表格的第一行(A1,C1,E1)。

任何人都可以帮助我编辑这段代码并解释它是如何工作的吗? :)

提前感谢你。

Option Explicit

Private Sub CommandButton1_Click()
Dim Ar() As String
Dim rng As Range, cl As Range
Dim i As Long

Set rng = Range("A1,C1,E1")

i = 1

For Each cl In rng
    ReDim Preserve Ar(1, 1 To i)
    Ar(1, i) = cl.Value
    i = i + 1
Next

With ListBox1
    .ColumnCount = i - 1
    .ColumnWidths = "50;50;50"
    .List = Ar
End With
End Sub

1 个答案:

答案 0 :(得分:0)

也许

Private Sub CommandButton1_Click()
   Dim rng As Range

   Set rng = Range("A1:E10")

   With ListBox1
      .ColumnCount = 3
      .ColumnWidths = "50;50;50"
      ' load 1st, 3rd and 5th columns of range into listbox
      .List = Application.Index(rng, Evaluate("ROW(1:" & rng.Rows.Count & ")"), Array(1, 3, 5))
   End With
End Sub