Last Selection from MultiSelect Listbox

B

Brian

Is there an easy way to identify the last item selected in a multiselect
listbox?

The MultiSelect property for the listbox I'm using is "MultiSelectMulti"
with a "liststyleOption".

Thanks,
Brian
 
J

Jim Cone

Brian,
'-----------------------------
Private Sub CommandButton2_Click()
Dim N As Long
Dim lngItem As Long
For N = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(N) = True Then
lngItem = N
End If
Next 'N
If lngItem > 0 Then
MsgBox "Last item selected is " & ListBox1.List(lngItem, 0)
Else
MsgBox "Nothing selected "
End If
End Sub
'------------------------------
Jim Cone
San Francisco, USA


Is there an easy way to identify the last item selected in a multiselect
listbox?
The MultiSelect property for the listbox I'm using is "MultiSelectMulti"
with a "liststyleOption".
Thanks,
Brian
 
J

Jim Cone

This works even better...

Private Sub CommandButton2_Click()
Dim N As Long
Dim lngItem As Long
lngItem = -1 ' CORRECTION
For N = 0 To Me.ListBox1.ListCount - 1
If ListBox1.Selected(N) = True Then
lngItem = N
End If
Next 'N
If lngItem > (-1) Then ' CORRECTION
MsgBox "Last item selected is " & ListBox1.List(lngItem, 0)
Else
MsgBox "Nothing selected "
End If
End Sub
 
B

Brian

Jim,

Sorry, but I must not have expained it correctly...

I'm not looking for the last item in the listbox that is selected.... I'm
looking for the last item actually selected (order of selection). And to
take it further... if something is deselected, I would like to refer back to
the previous item selected.

It might be easier for me to carry two single select listboxes with add> and
<remove buttons to control the "selected" list. If I don't sort the "Add
to" listbox... then the last item in that listbox is always the last item
selected (based on order).

But I thought that there might be a special function that would give me the
last item in the listbox that was selected.

Appreciate the post though.

Brian
 
G

Gman

There is no innate functionality that gives this unfortunately.

What you can do is track the listbox selection in an array by modifying
an array every time the listbox is changed. Here's an example of how to
do this. (It could all be done in the change event of course but I
decided to reuse some existing functions I had knocking around.)

To get the last selection made you simple use
mySelection(ubound(myselection)). If that is deselected then
mySelection(ubound(myselection)) still gets you the one before that.

I've used Caption1 and ListBox1 on my userform


Option Explicit

Private mySelection() As Integer

Private Sub UserForm_Initialize()
With ListBox1
.AddItem "Monkey"
.AddItem "Hippo"
.AddItem "Giraffe"
.AddItem "Cat"
.AddItem "Emu"
End With
End Sub

Private Sub ListBox1_Change()
Dim idx As Integer, i As Integer

'go through the ListBox and compare what's selected
'with what's in our array
With ListBox1
For i = 0 To .ListCount - 1
'is this item in our array?
idx = fcnArrayFindIndex(mySelection, i)
If idx > -1 Then
If Not .Selected(i) Then
DeleteItemFromArray mySelection, idx
Exit For 'there'll only be one change at once
End If
Else 'it's not in the array
If .Selected(i) Then
'add it to our array
fcnArrayAddItemTo mySelection, i
Exit For 'there'll only be one change at once
End If
End If

Next i
End With

'display our results
Label1.Caption = Empty
If fcnIsInitialisedArray(mySelection) Then
For i = 0 To UBound(mySelection)
Label1.Caption = Label1.Caption & mySelection(i) & vbCrLf
Next i
End If

End Sub

Public DeleteItemFromArray(ByRef arr() As Integer, idx As Integer)
If UBound(arr) = 0 Then
Erase arr
ElseIf idx = UBound(arr) Then
ReDim Preserve arr(idx - 1)
Else
Dim newarr() As Integer
Dim i As Integer, iNew As Integer
ReDim newarr(UBound(arr) - 1)
For i = 0 To UBound(arr)
If i <> idx Then
newarr(iNew) = arr(i)
iNew = iNew + 1
End If
Next i
arr = newarr
End If

End Sub
'Finds an item in an array
Public Function fcnArrayFindIndex(ByRef myArray() As Integer, myValue As
Variant) As Integer
Dim i As Integer

If fcnIsInitialisedArray(myArray) Then
For i = LBound(myArray) To UBound(myArray)
If CStr(myArray(i)) = CStr(myValue) Then
fcnArrayFindIndex = i
Exit Function
End If
Next i
End If
fcnArrayFindIndex = -1

End Function

'Adds an item to a passed array
Public Function fcnArrayAddItemTo(ByRef myArray() As Integer, myValue As
Variant) As Boolean

On Error GoTo ErrorHandler

If Not fcnIsInitialisedArray(myArray) Then
ReDim myArray(0)
myArray(0) = myValue
Else
ReDim Preserve myArray(UBound(myArray) + 1)
myArray(UBound(myArray)) = myValue
End If

fcnArrayAddItemTo = True

ErrorHandler:
On Error GoTo 0

End Function
Function fcnIsInitialisedArray(ByRef arr() As Integer) As Boolean
If IsArray(arr) Then
On Error Resume Next
If UBound(arr) < 0 Then
Else
fcnIsInitialisedArray = True
End If
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top