how to initialize more than one Listboxes in the same user form

pimp_mentality

New Member
Joined
Oct 15, 2015
Messages
46
Hey guys, need some assistance

I have a UserForm form which currently, when opened it displays all data in the workbook (5 columns). This is the code I am using to accomplish this.
Code:
Private Sub UserForm_Initialize()
    Dim oneCell As Range, LastRow As Long
    Dim MyArray As Variant
    Dim i As Integer


    ListBox1.ColumnCount = 5
    ListBox1.ColumnWidths = "180;250;180;0;100"
    
    LastRow = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row
    
    With ListBox1
        For Each oneCell In Sheet1.Range("A2").Resize(LastRow, 1)
            .AddItem
            .List(ListRow, 0) = Cells(oneCell.Row, 1).Value
            .List(ListRow, 1) = Cells(oneCell.Row, 2).Value
            .List(ListRow, 2) = Cells(oneCell.Row, 3).Value
            .List(ListRow, 4) = Cells(oneCell.Row, 5).Value
          
            ListRow = ListRow + 1
        Next oneCell
    End With


End Sub
What I would like to do now is have a second listbox which populates with data from columns, say A and B only from sheet2 when the user form is open.

Any suggestions.


ALSO

the above code is suppose to display 5 columns (A-E, these are the only columns with data) however I notice on initialization column D does not show in the list box. any help in rectifying this issues would also be appreciated
 
Last edited:

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
You showed:
ListBox1.ColumnWidths = "180;250;180;0;100"

Your telling the script to make column four width Zero

<strike></strike>
 
Last edited:
Upvote 0
To fill the five column list box will all the entries from rows 2 thru lastRow

Code:
With Sheet1.Range("A:A")
    Listbox1.List = Range(.Cells(2, 5), .Cells(Rows.Count,1).End(xlup)).Value
End With
 
Last edited:
Upvote 0
I changed the columns width:

Modify to your liking:

Try this:

Code:
Private Sub UserForm_Initialize()
Dim lastrow As Long
Dim lastrowa As Long
ListBox1.ColumnCount = 5
ListBox1.ColumnWidths = "100;100;100;100;100"
lastrow = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row
Data = Range("A2:E" & lastrow)
ListBox1.List = Data
'Listbox2
ListBox2.ColumnCount = 2
ListBox2.ColumnWidths = "100;100"
lastrowa = Sheet2.Cells(Rows.Count, "A").End(xlUp).Row
Data2 = Sheet2.Range("A2:B" & lastrowa)
ListBox2.List = Data2
End Sub
 
Last edited:
Upvote 0
This works wonderfully, Thanks man however

on of the columns in listbox2 is suppose to display time however it does not it just displays decimal numbers eg. 0.4355633333. Any suggestions to have the actually time displayed in the box?
 
Upvote 0
If the third column is supposed to show time, you could use code like


Code:
Dim oneCell as Range
lastrowa = Sheet2.Cells(Rows.Count, "A").End(xlUp).Row

With ListBox2
    .Clear
    For Each oneCell in Sheet2.Range("A2:A" & lastrowa).Rows
        .AddItem oneCell.Value
        .List(.ListCount, 1) = Format(oneCell.Offset(0,1).Value, "hh:mm:ss")
    Next oneCell
End With
 
Upvote 0
In your original post you said:
What I would like to do now is have a second listbox which populates with data from columns, say A and B only from sheet2 when the user form is open.

Then in post # 5 you said:
on of the columns in listbox2 is suppose to display time however it does not it just displays decimal numbers eg. 0.4355633333.

You did not say what column has time. Is it column A or column B?

<strike></strike>
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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