简体   繁体   中英

Display a non contiguous range of cells to a list box

I'm trying to display a non contiguous range of cells in a table called "Departments" (A1:A10,C1:C10,E1:E10) to a multicolumn Listbox.

I found an example here, but it only displays the first row of the table (A1,C1,E1).

Can anyone plz help me to edit this code and explain how it works ? :)

Thank u in advance.

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

perhaps

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM