Get selected items on list


Since function getListBoxSelectedItems uses some non-built VBA functions, they also must be included in your code for the function to work properly.

Otherwise the following error will occur: Compile error: Sub or Function not defined.

Required functions are listed below. You can get to each function's source code by clicking its name:

When adding the functions above to your VBA project, make sure you haven't done it before. If there are two different public functions with the same name in a single VBA project, the following compilation error is thrown: Compile error: Ambiguous name detected: function_name.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'**********************************************************************************************************
' Name:                 getListBoxSelectedItems
'
' Description:          Function to get all the items selected in the given ListBox as a String array.
'
' Parameters:
'   listbox             List box controls from MSForms library.
'
' Returns:
'   String()            String array containing all the items that are currently selected.
'
'
' --- Changes log -----------------------------------------------------------------------------------------
' 2015-02-25        tm              Function created.
'**********************************************************************************************************
Public Function getListBoxSelectedItems(listBox As MSForms.listBox) As String()
    Const METHOD_NAME As String = "getListBoxSelectedItems"
    '------------------------------------------------------------------------------------------------------
    Dim result() As String
    Dim i As Long
    '------------------------------------------------------------------------------------------------------


    'Iterate through all the items in the given ListBox. ------------------------------------------------|
    For i = 0 To listBox.ListCount - 1                                                                  '|
                                                                                                        '|
        'For each item check if it is selected. If it is, add it to the result array. ---------------|  '|
        If listBox.selected(i) Then                                                                 '|  '|
            Call addEntry(result, listBox.list(i))                                                  '|  '|
        End If                                                                                      '|  '|
        '--------------------------------------------------------------------------------------------|  '|
                                                                                                        '|
    Next i                                                                                              '|
    '----------------------------------------------------------------------------------------------------|


    'Assign [result] array to the function result.
    getListBoxSelectedItems = result


End Function