Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / April 2008

Tip: Looking for answers? Try searching our database.

ListBox1 - returns no value

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steven - 18 Apr 2008 02:28 GMT
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
-------------------------------------------------------------------
Jon Peltier - 18 Apr 2008 02:55 GMT
I don't know why you're having the problems. I do know you can streamline
the code.

A slight improvement comes by replacing this:

   'Add the sorted array back to the listbox
   For i = LBound(vaItems, 1) To UBound(vaItems, 1)
       oLb.AddItem vaItems(i, 0)
   Next i

with this:

 oLB.List = vaItems

A more substantial improvement comes if you sort right in the listbox:

Sub SortListBox(oLb As MSForms.ListBox)
   Dim i As Long, j As Long
   Dim vTemp As Variant

   'with VBA to sort the array
   For i = 0 To oLb.ListCount - 2
       For j = i + 1 To oLb.ListCount - 1
           If CStr(oLb.List(i, 0)) > CStr(oLb.List(j, 0)) Then
               vTemp = CStr(oLb.List(i, 0))
               oLb.List(i, 0) = CStr(oLb.List(j, 0))
               oLb.List(j, 0) = vTemp
           End If
       Next j
   Next i
End Sub

I did this with a form that had the listbox and two buttons, one which ran
the sorting code and selected the first item, the other that popped up the
message box. The message box always showed me the first item in the listbox.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______

>I am doing the following:
>
[quoted text clipped - 38 lines]
> End Sub
> -------------------------------------------------------------------
Steven - 18 Apr 2008 20:31 GMT
Thank you for your response.  By looking at your suggestion it made me think
harder about it and I noticed that after the sort and the set index to 0 that
the listbox did not have the focus so then I just had to set the focus back
tot he list box and now everything is ok.

Thank you,

Steven

> I don't know why you're having the problems. I do know you can streamline
> the code.
[quoted text clipped - 81 lines]
> > End Sub
> > -------------------------------------------------------------------
Jon Peltier - 18 Apr 2008 22:29 GMT
Whether the listbox has focus or not, it still has the same value.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______

> Thank you for your response.  By looking at your suggestion it made me
> think
[quoted text clipped - 99 lines]
>> > End Sub
>> > -------------------------------------------------------------------
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.