+ Reply to Thread
Results 1 to 9 of 9

Fill Listbox with 2 sheet columns.

Hybrid View

  1. #1
    Registered User
    Join Date
    08-27-2009
    Location
    Milton Keynes
    MS-Off Ver
    Excel 2007
    Posts
    45

    Fill Listbox with 2 sheet columns.

    Hi there,

    I'm trying to populate a Listbox "listbox3" with two columns on a worksheet. These columns aren't set next to one another, therefore I cannot just assign a rowsource of "A:B" etc.. it would instead need to be "D" and "F".

    I have started a piece of code (below) however not sure if I'm going about it the right way. Any advice would be greatly appreciated.

     Dim rngFirstNme As Range
    Dim rngLastNme As Range
    
    rngFirstNme = Sheets("pgs_CustomerList_Unique").Range("D2:D502").Value
    
    rngLastNme = Sheets("pgs_CustomerList_Unique").Range("F2:F502").Value
    
    
    Listbox3. ? = ?
    Last edited by RayJay01; 12-27-2013 at 04:51 PM.

  2. #2
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Fill Listbox with 2 sheet columns.

    It is simplest to assign the three columns to the list box and make the second column width 0. You may also create an array from the two columns and assign it to the List property of the Listbox but I do not see benefit over simply using three columns.
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  3. #3
    Registered User
    Join Date
    08-27-2009
    Location
    Milton Keynes
    MS-Off Ver
    Excel 2007
    Posts
    45

    Re: Fill Listbox with 2 sheet columns.

    Sorry to be a pain, but could you give me an example of what you mean please?

  4. #4
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Fill Listbox with 2 sheet columns.

    Example of which option?

  5. #5
    Registered User
    Join Date
    08-27-2009
    Location
    Milton Keynes
    MS-Off Ver
    Excel 2007
    Posts
    45

    Re: Fill Listbox with 2 sheet columns.

    I'll go with the Array please. Only because I think it may help to learn how best to use an array, I'm sure this will come in handy a lot. I've tried to create a version below, but it still doesn't work. Loads a two column listbox, just no data inside it... =\

    
    Dim i As Integer
    
    Dim data(1 To 500, 1 To 2)
    
    For i = 1 To 500
    
        data(i, 1) = Sheets("pgs_CustomerList_Unique").Range("D2:D502").Value
    
    Next i
    
    For i = 1 To 500
    
        data(i, 2) = Sheets("pgs_CustomerList_Unique").Range("F2:F502").Value
    
    Next i
    
    ListBox2.ColumnCount = 2
    ListBox2.List = data

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Fill Listbox with 2 sheet columns.

    Try this.
    Private Sub Userform_Initialize()
    Dim arr As Variant
    
          arr = Sheets("pgs_CustomerList_Unique").Range("D2:F502").Value
          With ListBox2
                 .ColumnCount = 3
                 .ColumnWidths = "50;0;50"
                 .List = arr
         End With
    
    End Sub
    If posting code please use code tags, see here.

  7. #7
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Fill Listbox with 2 sheet columns.

    Norie has given example for three columns with one hidden. If you wish array from two columns you may use code like this:
    ListBox2.ColumnCount = 2
    ListBox2.List = Application.Index(Sheets("pgs_CustomerList_Unique").Range("D2:F502"), [Row(1:501)], array(1, 3))

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Fill Listbox with 2 sheet columns.

    Izandol

    I don't think you need the row argument, as far as I know if you leave it out INDEX will return the entire column(s) from the reference range.

  9. #9
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Fill Listbox with 2 sheet columns.

    Norie

    If you pass array for third argument you must use array for second - using 0 or omitting does not work unfortunately.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Macro to fill 3 columns in one sheet with data from 3 cells in another sheet
    By programct in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-30-2013, 02:44 PM
  2. Replies: 12
    Last Post: 08-28-2012, 07:09 AM
  3. Replies: 4
    Last Post: 04-05-2012, 11:30 AM
  4. Copy data from Listbox columns to Sheet
    By WCJanssen in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-30-2011, 09:50 AM
  5. ListBox Fill 3 Columns With Worksheet Range's
    By radar_jynx in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-10-2009, 09:25 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1