• Fill MultiColumn Listbox With Worksheet Range

    Introduction:
    This article gives two methods to fill a multi column listbox on a userform with data from an Excel worksheet.The intention of both of these pieces of code is to be used in a userform. Both are set up in the Userform_Intialize event, although only one or other should be used.

    Versions Tested:
    These routines were tested successfully running Office 2003, but shouldn't have any issue in Excel 97 or higher.

    Pulling the worksheet range directly into a listbox
    Code:
    Private Sub UserForm_Initialize()
    'Author       : Ken Puls (www.excelguru.ca)
    'Macro Purpose: To populate a multi-column listbox with data from
    '               a worksheet range
        
        Dim lbtarget As MSForms.ListBox
        Dim rngSource As Range
        
        'Set reference to the range of data to be filled
        Set rngSource = Worksheets("Sheet1").Range("A2:C5")
        
        'Fill the listbox
        Set lbtarget = Me.ListBox1
        With lbtarget
            'Determine number of columns
            .ColumnCount = 3
            'Set column widths
            .ColumnWidths = "50;80;100"
            'Insert the range of data supplied
            .List = rngSource.Cells.Value
        End With
        
    End Sub
    Pulling the data into an array first, then placing the array in the listbox
    Code:
    Private Sub UserForm_Initialize()
    'Author       : Ken Puls (www.excelguru.ca)
    'Macro Purpose: To populate a multi-column listbox with data from
    '               a worksheet range
        Dim lb As msforms.ListBox
        Dim rcArray() As Variant
        Dim lrw As Long, lcol As Long
        Dim rngTarget As Range
        
        'Define the range you want to use
        Set rngTarget = Worksheets("Sheet1").Range("A2:C5")
        
        'Set the boundaries of the array
        ReDim Preserve rcArray(1 To rngTarget.Rows.Count, 1 To rngTarget.Columns.Count)
        
        'Fill the array with data from the worksheet
        With rngTarget
            For lcol = 1 To .Columns.Count
                For lrw = 1 To .Rows.Count
                    rcArray(lrw, lcol) = rngTarget.Cells(lrw, lcol)
                Next lrw
            Next lcol
        End With
        
        'Place the array in the listbox
        Set lb = Me.ListBox1
        With lb
            .ColumnCount = 3
            .ColumnWidths = "50;80;100"
            .List = rcArray
        End With
        
    End Sub
    Example File:
  • MVP Logo
  •  Donations

    If you like our website and would like to give something in return, you can make a donation. All donations are gratefully received and go to support the site.


    Select your preferred currency and donation amount, then click the donate button.

  • Recent Forum Posts

    Colo

    How to Rename a File from English to Other Language?

    Yeah, some massive HTML conversion is my line. In other words, most of difficult things can be done with Excel alone like this time. Well done, Excel!...

    Colo Today, 07:36 AM Go to last post
    Ken Puls

    Link a series name to a cell using Excel 2010 VBA

    Hi there,

    I recorded linking the title to a cell and it came back with the following. Does this help?

    Code:
        ActiveChart.SetElement
    ...

    Ken Puls Today, 04:06 AM Go to last post
    Ken Puls

    How to Rename a File from English to Other Language?

    Colo, that was way too easy... I was expecting some massive HTML conversion, or a huge engine to compare each character against a library of Chr codes!...

    Ken Puls Today, 04:02 AM Go to last post
    Ken Puls

    Shared file\macro & IP address

    Oh, and as for the max number of users who can access the file in the shared folder at once...

    • For reading, I believe it's unlimited. (The second and
    ...

    Ken Puls Today, 03:59 AM Go to last post
    Ken Puls

    Shared file\macro & IP address

    ibrahimaa,

    There is no one-line way to get your IP address the way you are getting the username. So you're going to need more code than...

    Ken Puls Today, 03:56 AM Go to last post