Excel

ListBox Control - Determine the Order in Which Selections are Made

Ease of Use

Intermediate

Version tested with

X (Mac), 2003 

Submitted by:

BlueCactus

Description:

A demonstration of determining the order in which a user makes selections in a ListBox control with MultiSelect enabled. 

Discussion:

ListBox controls can have MultiSelect set to 1 or 2 to enable multiple selections by the user. While you can determine whether ListBox.List(i) is selected using the boolean value ListBox.Selected(i), you cannot directly find out the order in which the user made the selections. This KB demonstrates how to achieve that goal by attaching some code to the ListBox_Change() event. The code is commented, but it is worth first knowing that a) the integer array listSelect() contains the selection number for each item in the ListBox, where 0 = not selected, 1 = first selected, 2 = second selected, etc... b) the integer totalSelect contains the total number of selected items in the ListBox. This code works with all values of .MultiSelect (try changing it in the Userform_Initialize() code) but it is most valuable with .MultiSelect = 2. 

Code:

instructions for use

			

'----------------------------------------------------------------- '/// This MUST go at module level in the UserForm (i.e., be the first thing in the code): '----------------------------------------------------------------- Option Explicit Dim listSelect() As Integer, totalSelect As Integer, eventsEnabler As Integer '----------------------------------------------------------------- '/// Wherever you set up the ListBox with its contents, include the following code. '/// This is often, but not always, UserForm_Initialize() '/// In this example, our ListBox is called ListBox1. Change references as appropriate '----------------------------------------------------------------- ' Disable ListBox1_Change() code eventsEnabler = 1 With ListBox1 ' .AddItems here End With ReDim listSelect(0 To ListBox1.ListCount - 1) totalSelect = 0 ' Enable _Change() code eventsEnabler = eventsEnabler - 1 '----------------------------------------------------------------- '/// This is the key section that needs to be in the UserForm code: '----------------------------------------------------------------- Private Sub ListBox1_Change() Dim i As Integer, j As Integer, indexStart As Integer, indexEnd As Integer ' When setting up ListBox1, set eventsEnabler to > 0 to prevent this code from executing! If eventsEnabler = 0 Then ' Start by assuming we'll search for new selections top-to-bottom indexStart = 0 indexEnd = ListBox1.ListCount - 1 ' This block determines direction of additional selections (up or down) For i = 0 To ListBox1.ListCount - 1 ' If we've hit the previous last selection, we already know the answer (top-to-bottom) If listSelect(i) = totalSelect Then Exit For ' If we hit a new selection before finding the previous last selection, ' then we'll change the search for new selections to be bottom-to-top If listSelect(i) = 0 And ListBox1.Selected(i) Then indexStart = indexEnd indexEnd = 0 Exit For End If Next i stepDir = Sgn(indexEnd - indexStart) If stepDir = 0 Then stepDir = 1 ' Update selection list in listSelect() For i = indexStart To indexEnd Step stepDir If ListBox1.Selected(i) = True Then ' Newly selected item: place selection number in listSelect() and update totalSelect If listSelect(i) = 0 Then listSelect(i) = totalSelect + 1 totalSelect = totalSelect + 1 End If Else ' Deselected item: remove its selection number, and update all others to compensate. If listSelect(i) > 0 Then For j = 0 To ListBox1.ListCount - 1 If listSelect(j) > listSelect(i) Then listSelect(j) = listSelect(j) - 1 Next j listSelect(i) = 0 totalSelect = totalSelect - 1 End If End If Next i ' At this point, totalSelect and listSelect() are updated for you to act upon. ' The example file includes a call to update the ListBox contents based on these values. End If End Sub

How to use:

  1. Download the sample file, unzip it and open it.
  2. Test using (Menus) Tools -> Macro -> Macros... -> test -> Run
  3. The important code that you need to implement is shown above.
  4. Copy and Paste this code to your own UserForm code using the instructions in the comments.
  5. Examine the contents of the example file code if you need help understanding how to integrate the above code.
 

Test the code:

  1. Test sample file code using: (Menus) Tools -> Macro -> Macros... test -> Run
  2. Try selecting/deselecting individual and multiple items in the ListBox.
  3. Click on the 'Done' button to finish.
 

Sample File:

listbox_selection_order.xls.zip 15.25KB 

Approved by mdmackillop


This entry has been viewed 369 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express