• If you would like to post, please check out the MrExcel Message Board FAQ and register here. If you forgot your password, you can reset your password.
  • Excel articles and downloadable files provided in the articles have not been reviewed by MrExcel Publishing. Please apply the provided methods / codes and open the files at your own risk. If you have any questions regarding an article, please use the Article Discussion section.
Dermot

Code to allow multi select listboxes

Excel Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
  5. 2011
  6. 2010
  7. 2007
Using the Developer ribbon, Excel allows you to add listboxes and even to make them multi-select, but doesn't give you a way of listing the multiple selections in the worksheet.

If you set your listbox up like this
2020-07-25 21_38_27-Format Control.jpg

your cell link will just show 0 - basically the listbox breaks.
(I know the cell link is missing above, but it doesn't make a difference)

The code below will fix this by showing the user selections in the column to the right of the list of items in the worksheet, as shown below. All you do is set the listbox up as shown above (no need to include a cell link), then right click on the box and assign the macro below.

2020-07-25 21_53_14-fredt.xlsb - Excel.jpg


VBA Code:
'gets selected items in a multi select list box and lists them next to the original list of items in the worksheet
Sub getUserSelections()
  Dim V As Variant, V2 As Variant
  Dim i As Long, n As Long, lb As Long, adjust As Long
  With ActiveSheet.ListBoxes(Application.Caller)
    V = .Selected 'get 1-D array of selected status, TRUE/FALSE
    n = UBound(V)
    ReDim V2(n, 1)
    lb = LBound(V2, 2) 'the lower bound will be 0 unless Option base 1 is set
    'if it is zero, then we need to be careful because the list in V has a lower bound of 1
    If lb = 0 Then adjust = 1
    For i = lb To n - adjust
      V2(i, lb) = V(i + adjust)
    Next i
    'paste array
    Range(.ListFillRange).Cells(1, 1).Offset(0, 1).Resize(n, 1) = V2
  End With
Author
Dermot
Views
3,178
First release
Last update
Rating
5.00 star(s) 1 ratings

More Excel articles from Dermot

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