Selectively populating a multicolumn listbox

JimmyBob

New Member
Joined
Sep 25, 2011
Messages
33
Hi folks. I have a multicolumn listbox (8 columns). Is it possible to selectively populate only Listbox columns 1-4 with e.g. C1:F30 from a spreadsheet using the Sheets .Range property? I want to bypass column 0.

The following, by default, populates columns 0-3, can I change this to populate columns 1-4 in a similar way?

With ListBox1
.ColumnCount = 8
.List = Sheets("Sheet1").Range("C1:F30").Value
End With

I could use other methods to populate the listbox e.g. .additem or .list(row,column)= ,but I'm interested to see if I can populate it in one step.

I'll be using the remaining columns to display various things such as debug info relating to each record/row

Thanks.
 
Last edited:

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Jerry Sullivan

MrExcel MVP
Joined
Mar 18, 2010
Messages
8,787
Hi JimmyBob,

You could build a variant array that would add an empty column(s) to the left of the data,
then populate the ListBox using that array.

Code:
Sub PopulateListBox()
    Dim vArray As Variant
    Dim lOffsetCols as Long

    vArray = Sheets("Sheet1").Range("C1:F30").Value

    
    With ListBox1
        .ColumnCount = 8
        .List = OffsetArray(vArray, 1) 'offset 1 column
    End With

 
End Sub

Code:
Function OffsetArray(vArrIn As Variant, lOffsetCols) As Variant
'---resizes array and offsets to add variable number of blank columns to a variant array

    Dim iR As Long, iC As Long
    Dim vArrOut As Variant

    
    ReDim vArrOut(1 To UBound(vArrIn, 1), 1 To UBound(vArrIn, 2) + lOffsetCols)

    
    For iR = 1 To UBound(vArrIn, 1)
        For iC = 1 To UBound(vArrIn, 2)
              vArrOut(iR, iC + lOffsetCols) = vArrIn(iR, iC)
        Next iC
    Next iR

    
    OffsetArray = vArrOut
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,196,007
Messages
6,012,831
Members
441,732
Latest member
Ayon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top