Excel Pragma

January 18, 2005

Filling a Multi-Column Listbox

Filed under: Forms, General — Admin @ 7:57 am

Filling a one-column listbox is a relatively straight-forward job. Use the .AddItem method of a ListBox and you’re done.

Filling a multi-column Listbox is also as simple, if you know the right command.

Basically, what .AddItem does is add a row to the bottom of the list. Now what you need to do is use .List to assign the values to the 2nd, 3rd or more column.

The following code adds the values “RowXcolumn1”,“RowXcolumn2”,“RowXcolumn3” to a 3–column listbox lstMyListBox.

    Const NumColumns = 3
    Const NumRows = 10
    
    Dim i As Integer, j As Integer
    
    For i = 1 To NumRows
        lstMyListBox.AddItem “Row” & i & “Column1″
        For j = 1 To NumColumns - 1
            lstMyListBox.List(i - 1, j) = “Row” & i & “Column” & j
        Next j
    Next i

Remember to set the ColumnCount property of the ListBox to the number of columns you need. if you’re trying this example, set the ColumnWidths property of the listbox of 75pt.

 

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress