Displaying Multiple columns in a Listbox on Userform initialization

pimp_mentality

New Member
Joined
Oct 15, 2015
Messages
46
Good Day Guys.

I currently use the following line of code to populate listbox1 when the userform is open

Code:
ListBox1.List = Sheets("PI").Range("Names2", Sheets("PI").Cells(Rows.Count, "B").End(xlUp)).Value

The thing is, this code only displays data in column B.

For the purposes of this project I would like data from columns B, E, F, H (in that order) to be displayed when the Userform is opened. To accomplish this I tried the following with zero success:

Code:
ListBox1.List = Sheets("PI").Range("Names2", Sheets("PI").Cells(Rows.Count, "B", "E", "F" and "H").End(xlUp)).Value

I would appreciate assistance re modifying the above code to accomplish this.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hope this helps.

Code:
Private Sub UserForm_Initialize()
    Dim i As Long
    With ListBox1
        For i = 2 To Sheets("PI").cells(Rows.count, 2).End(xlUp).row
            .AddItem ""
            .list(.ListCount - 1, 0) = cells(i, 2)
            .list(.ListCount - 1, 1) = cells(i, 5)
            .list(.ListCount - 1, 2) = cells(i, 6)
            .list(.ListCount - 1, 3) = cells(i, 8)
        Next i
    End With
End Sub
 
Upvote 0
Try this:
Code:
Private Sub UserForm_Initialize()
'B,E,F,H
Dim i As Long
Dim b As Long
Dim c As Long
b = 0
ListBox1.ColumnCount = 4
ListBox1.ColumnWidths = "100;100;100;100"
Dim Del As Variant
Del = Array(2, 5, 6, 8)
    For c = 0 To 3
    For i = 1 To Sheets("PI").Cells(Rows.Count, Del(c)).End(xlUp).Row
        With ListBox1
            .AddItem
            .List(b, c) = Sheets("PI").Cells(i, Del(c)).Value
            
                b = b + 1
        End With
    
    Next
    b = 0
    
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top