Wilder1626
asked on
Populate listbox in Excel
Hello all,
I would like to populate my VBA ListBox1.
I need to separate all 5 columns of data.
The data source is in Sheets("Templates") .Range("A21:E48")
How can i do this please?
Thanks for your help.
I would like to populate my VBA ListBox1.
I need to separate all 5 columns of data.
The data source is in Sheets("Templates") .Range("A21:E48")
How can i do this please?
Thanks for your help.
Listboxes do not have that capability, I'm afraid.
ASKER
Is there another option?
Like a listview?
Like a listview?
You can get gridlines with a ListView but not just column dividers - I don't know if that would work for you?
ASKER
yes it would work
Thanks for your help
Thanks for your help
ASKER
How can we do this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have a run time error 438.
It does not want to open my userform.
Do you know why?
It does not want to open my userform.
Do you know why?
No, since I don't know what code is in your userform or how you are opening it. If you press Debug when you get the error, then f8 to step through the code, which line actually causes the error? (I assume you added a ListView control called ListView1 to your form).
ASKER
The section that give the error is that part:
For lngRow = 1 To rngSource.Rows.Count
Set itm = .ListItems.Add(lngRow, , rngSource.Cells(lngRow, 1))
For lngCol = 2 To rngSource.Columns.Count
itm.ListSubItems.Add , , rngSource.Cells(lngRow, lngCol)
Next lngCol
Next lngRow
For lngRow = 1 To rngSource.Rows.Count
Set itm = .ListItems.Add(lngRow, , rngSource.Cells(lngRow, 1))
For lngCol = 2 To rngSource.Columns.Count
itm.ListSubItems.Add , , rngSource.Cells(lngRow, lngCol)
Next lngCol
Next lngRow
ASKER
Even more:
only that part:
' Set itm = .ListItems.Add(lngRow, , rngSource.Cells(lngRow, 1))
'For lngCol = 2 To rngSource.Columns.Count
'itm.ListSubItems.Add , , rngSource.Cells(lngRow, lngCol)
' Next lngCol
only that part:
' Set itm = .ListItems.Add(lngRow, , rngSource.Cells(lngRow, 1))
'For lngCol = 2 To rngSource.Columns.Count
'itm.ListSubItems.Add , , rngSource.Cells(lngRow, lngCol)
' Next lngCol
Which line specifically? I tested before posting and it ran fine for me (XL2003).
Can you post your workbook (censor any confidential data)?
ASKER
As soon as i remove the apostrophe from all these lines:
' Set itm = .ListItems.Add(lngRow, , rngSource.Cells(lngRow, 1))
'For lngCol = 2 To rngSource.Columns.Count
'itm.ListSubItems.Add , , rngSource.Cells(lngRow, lngCol)
' Next lngCol
it creates an error and it close the excel spreadsheet
' Set itm = .ListItems.Add(lngRow, , rngSource.Cells(lngRow, 1))
'For lngCol = 2 To rngSource.Columns.Count
'itm.ListSubItems.Add , , rngSource.Cells(lngRow, lngCol)
' Next lngCol
it creates an error and it close the excel spreadsheet
ASKER
How can i post it?
Click the File link at the bottom of the comment box, then upload your file.
ASKER
What control did you have on that form? I got an error about an unavailable object and it was then removed. When I added a Microsoft ListView control 6.0 control, the code ran fine.
ASKER
It' working in another excel spreadsheet but not in my real one.
Really don't understand.
Really don't understand.
I've just tested on another machine just to be sure, and had the same problem.
Have you tried removing the control and re-adding a new ListView?
Have you tried removing the control and re-adding a new ListView?
ASKER
Thanks for your help
ASKER
Dim lbtarget As MSForms.ListBox
Dim rngSource As Range
'Set reference to the range of data to be filled
Set rngSource = Worksheets("DB").Range("A2
'Fill the listbox
Set lbtarget = Me.Listbox1
With lbtarget
'Determine number of columns
.ColumnCount = 5
'Set column widths
.ColumnWidths = "120;120;120;120;120"
'Insert the range of data supplied
.List = rngSource.Cells.Value
End With
Now, i just need to separate with a line between the columns