Link to home
Start Free TrialLog in
Avatar of Wilder1626
Wilder1626Flag for Canada

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.
Avatar of Wilder1626
Wilder1626
Flag of Canada image

ASKER

Thast's where i am now:

Dim lbtarget As MSForms.ListBox
Dim rngSource As Range
   
    'Set reference to the range of data to be filled
    Set rngSource = Worksheets("DB").Range("A21:E48")
   
    '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
Listboxes do not have that capability, I'm afraid.
Is there another option?

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?
yes it would work

Thanks for your help
How can we do this?
ASKER CERTIFIED SOLUTION
Avatar of Rory Archibald
Rory Archibald
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have a run time error 438.

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).
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
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
Which line specifically? I tested before posting and it ran fine for me (XL2003).
Can you post your workbook (censor any confidential data)?
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
How can i post it?
Click the File link at the bottom of the comment box, then upload your file.
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.
It' working in another excel spreadsheet but not in my real one.

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?
Thanks for your help