Count selected items on list


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
'**********************************************************************************************************
' Name:                 countListBoxSelectedItems
'
' Description:          Function to count the currently selected items on the given ListBox.
'
' Parameters:
'   listbox             List box controls from MSForms library.
'
' Returns:
'   Integer             The number of currently selected items on the given ListBox.
'
'
' --- Changes log -----------------------------------------------------------------------------------------
' 2015-02-25        tm              Function created.
'**********************************************************************************************************
Public Function countListboxSelectedItems(listBox As MSForms.listBox) As Integer
    Const METHOD_NAME As String = "countListboxSelectedItems"
    '------------------------------------------------------------------------------------------------------
    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, increment result value by 1. --------------|  '|
        If listBox.selected(i) Then                                                                 '|  '|
            countListboxSelectedItems = countListboxSelectedItems + 1                               '|  '|
        End If                                                                                      '|  '|
        '--------------------------------------------------------------------------------------------|  '|
                                                                                                        '|
    Next i                                                                                              '|
    '----------------------------------------------------------------------------------------------------|


End Function