Re: Last Selection from MultiSelect Listbox
- From: Gman <nah>
- Date: Sat, 03 Dec 2005 01:13:14 -0600
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 SubPrivate 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 IfEnd 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 IfEnd 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 = -1End 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 IffcnArrayAddItemTo = True
ErrorHandler:
On Error GoTo 0End 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
On Error GoTo 0
End FunctionBrian wrote:
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
"Jim Cone" <jim.coneXXX@xxxxxxxxxx> wrote in message news:O64xLR89FHA.1444@xxxxxxxxxxxxxxxxxxxxxxx
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
"Brian" <bkstigler@xxxxxxx> wrote in message news:%23aEz%23u59FHA.3292@xxxxxxxxxxxxxxxxxxxx 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
.
- References:
- Last Selection from MultiSelect Listbox
- From: Brian
- Re: Last Selection from MultiSelect Listbox
- From: Jim Cone
- Re: Last Selection from MultiSelect Listbox
- From: Brian
- Last Selection from MultiSelect Listbox
- Prev by Date: Re: Adding a "1" in front of everything in column A
- Next by Date: Re: TextBox and Time
- Previous by thread: Re: Last Selection from MultiSelect Listbox
- Next by thread: RE: Set PivotItem using counter
- Index(es):