S
Steven
I am doing the following:
Call SortListBox(.ListBox1)
..ListBox1.ListIndex = 0
MsgBox .ListBox1.Value '*** IT RETURNS NOTHING *** WHY????
?? How do I get it to return the highlighted item (ie what I see as the 1st
item) without having to mouse click on it? I am not asking for the index
number, but what I actually see. It does not seem to recognize it as a
value until clicked but I want to return it without having to click on it.
Thank you for your help...Steven
-------------------------------------------------------
Sub SortListBox(oLb As MSForms.ListBox)
Dim vaItems As Variant
Dim i As Long, j As Long
Dim vTemp As Variant
'Put the items in a variant array
vaItems = oLb.List
'with VBA to sort the array
For i = LBound(vaItems, 1) To UBound(vaItems, 1) - 1
For j = i + 1 To UBound(vaItems, 1)
If vaItems(i, 0) > vaItems(j, 0) Then
vTemp = vaItems(i, 0)
vaItems(i, 0) = vaItems(j, 0)
vaItems(j, 0) = vTemp
End If
Next j
Next i
'Clear the listbox
oLb.Clear
'Add the sorted array back to the listbox
For i = LBound(vaItems, 1) To UBound(vaItems, 1)
oLb.AddItem vaItems(i, 0)
Next i
End Sub
-------------------------------------------------------------------
Call SortListBox(.ListBox1)
..ListBox1.ListIndex = 0
MsgBox .ListBox1.Value '*** IT RETURNS NOTHING *** WHY????
?? How do I get it to return the highlighted item (ie what I see as the 1st
item) without having to mouse click on it? I am not asking for the index
number, but what I actually see. It does not seem to recognize it as a
value until clicked but I want to return it without having to click on it.
Thank you for your help...Steven
-------------------------------------------------------
Sub SortListBox(oLb As MSForms.ListBox)
Dim vaItems As Variant
Dim i As Long, j As Long
Dim vTemp As Variant
'Put the items in a variant array
vaItems = oLb.List
'with VBA to sort the array
For i = LBound(vaItems, 1) To UBound(vaItems, 1) - 1
For j = i + 1 To UBound(vaItems, 1)
If vaItems(i, 0) > vaItems(j, 0) Then
vTemp = vaItems(i, 0)
vaItems(i, 0) = vaItems(j, 0)
vaItems(j, 0) = vTemp
End If
Next j
Next i
'Clear the listbox
oLb.Clear
'Add the sorted array back to the listbox
For i = LBound(vaItems, 1) To UBound(vaItems, 1)
oLb.AddItem vaItems(i, 0)
Next i
End Sub
-------------------------------------------------------------------