Populate an array with items in listbox

Eclektics

New Member
Joined
Jun 9, 2015
Messages
24
Hi All,

I have a user form with 2 list boxes, the left box is populated and the right is blank, users will select values on the left and move them to the right list box.

When they hit the command button I want all the selected items in the right list box (ListBox2) to populate an array that I can then use to filter the data in the workbook.

Here is my code so far, it errors on the line 'ListBox2.List = LArray'. Any help would be greatly appreciated. :)

Code:
Private Sub cmdSelect_Click()
Dim LArray As Long
Dim lrow, LRowII As Long
ListBox2.List = LArray
Dim ds, ws As Worksheet
Set ws = Sheet2
Set ds = Sheets("Excluded Data")
lrow = ws.Cells(ws.Rows.Count, "A:A").End(xlUp).Row
LRowII = ds.Cells(ds.Rows.Count, "A:A").End(xlUp).Row
With ds
    .Range("$A:$HH").AutoFilter field:=1, Criteria1:=LArray
    .Range("$A$2:$HH$" & LRowII).SpecialCells(xlCellTypeVisible).Copy Destination:=ws.Range("A" & lrow + 1)
    .Range("$A$2:$HH$" & LRowII).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    .ShowAllData
End With
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
If you wanted to populate LArray with the items from ListBox2 it would look like this.
Code:
LArray = ListBox2.List
 
Upvote 0
Hi Norie,

Thanks for replying so quick :) unfortunately I tried that prior to submitting the thread. When I had the code that way round I got a type mis-match error.
 
Upvote 0
Where did you get the type mismatch error?
 
Upvote 0
This
Code:
Dim LArray As Long
should be
Code:
Dim LArray As Variant
 
Upvote 0
Thanks for the help peeps, unfortunately still sturggiling. To answer your question Norie it errors on the line
ListBox2.List = LArray

Thanks for your response Fluff but I still got an error, a bit more googling and it advised change the .list to .value but that's not working either, I'm assuming this is because it takes all individual selections as one string. When chaining to variant I go the error ' could not set the list property'. Thanks for the help so far, really appreciate it :)
 
Upvote 0
If you have changed the declaration of LArray as Fluff suggested then this should work.
Code:
LArray = ListBox2.List

If you use .Value then you would only return the selected value in the listbox.
 
Upvote 0
Thanks Norie, I'd missed changing that part around so I had it as 'Listbox2.list = LArray'

Swapped that around and now it gets passed that part but throws up a new error of 'type-mismatch' on this line
.Range("$A:$HH").AutoFilter field:=1, Criteria1:=LArray

Sorry for adding to the original question but I've spent so much time on this now that every error I see makes me want to give up lol
<strike>
</strike>
 
Upvote 0
As far as I'm aware when filtering using an array of values you need a 1-dimensional array.

When you populate an array from a listbox you get a 2-dimensional array.

You could try transposing that array but I'm not sure that will work either - filtering by an array of values in code can sometimes be tricky.
Code:
.Range("$A:$HH").AutoFilter field:=1, Criteria1:=Application.Transpose(LArray)
 
Upvote 0
Thank you, I tried that amendment but still the same type mismatch error. It's suprising it's difficult as I thought most Excel users at some point would try to implement something like this.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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
Back
Top