Im trying to figure out how to add a non contiguous range of cells in a row to a multicolumn Listbox, but only the second line below works.
lbxSelectable.List = Sheets("DB").Range("A1,B1,C1").Value
lbxSelectable.List = Sheets("DB").Range("A1:C1").Value
Is there any efficient function which will allow me to select certain columns in a row and stick it in an entry of a multicolumn list box?
Many thanks
I. Display non contiguous column data without loops
In addition to Siddharth's valid solution I demonstrate a flexible approach without any loop necessity. Furthermore I added the possibility to display not only one, but even more data rows as well as to define a varying start row (e.g.
m=2
in this example:-).Applied method step by step
After fully qualifying a range reference in section
[0]
, defining start row and end row in section[1]
, section[2]
does the main work by assigning the wanted columns to a variant datafield array using advanced filtering possibilities of the ►Application.Index
method •):Application.Index
method.Section
[3]
fills the listbox with data from the array which have to be 2-dimensional. If data sets have more than one item 'rows' they get assigned to the ►.List
property of the listbox - cf. section [3]b).If, however there is only one single data row, the array
v
would loose one needed dimension, so it has to be transposed via theApplication.Transpose
method (changing rows and columns, but regaining the 2nd dimension!) and then assigned to the listboxes ►.Column
property.•) cf. Insert first column in datafield array without loops or API calls
II. Simple alternative with adapted
ColumnWidths
Of course, it would be possible to collect the whole data range columns
A:E
, but to define zero widths for the columns to be omitted in display via theColumnWidths
property:Enjoy it :-)
To display a non contiguous range of cells in a row to a multicollumn Listbox, you have to first create an Array and then assign it to the
.list
of the listbox. Here is an example.SNAPSHOT