|
|
|
|
|
|
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
|
Option Explicit
Dim listSelect() As Integer, totalSelect As Integer, eventsEnabler As Integer
eventsEnabler = 1
With ListBox1
End With
ReDim listSelect(0 To ListBox1.ListCount - 1)
totalSelect = 0
eventsEnabler = eventsEnabler - 1
Private Sub ListBox1_Change()
Dim i As Integer, j As Integer, indexStart As Integer, indexEnd As Integer
If eventsEnabler = 0 Then
indexStart = 0
indexEnd = ListBox1.ListCount - 1
For i = 0 To ListBox1.ListCount - 1
If listSelect(i) = totalSelect Then Exit For
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
For i = indexStart To indexEnd Step stepDir
If ListBox1.Selected(i) = True Then
If listSelect(i) = 0 Then
listSelect(i) = totalSelect + 1
totalSelect = totalSelect + 1
End If
Else
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
End If
End Sub
|
How to use:
|
- Download the sample file, unzip it and open it.
- Test using (Menus) Tools -> Macro -> Macros... -> test -> Run
- The important code that you need to implement is shown above.
- Copy and Paste this code to your own UserForm code using the instructions in the comments.
- Examine the contents of the example file code if you need help understanding how to integrate the above code.
|
Test the code:
|
- Test sample file code using: (Menus) Tools -> Macro -> Macros... test -> Run
- Try selecting/deselecting individual and multiple items in the ListBox.
- 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.
|
|