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 / Word / Programming / November 2006

Tip: Looking for answers? Try searching our database.

Problem using array to sort listbox items

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jay - 18 Nov 2006 23:23 GMT
OK, I'm a vba newbie, but a master of cut and paste. :D  I'm trying to
sort the items in a listbox on a form.  The listbox items are pulled
from part of a large text file, and change based on a combobox
selection.  I have everything working fine except the alphabetical
sort. The code works just fine if you know the number of elements in
the array. But since the number of elements I'm dealing with changes,
the number of elements in the array is unknown.

Here is the code that I found somewhere on the internet for doing the
sort - the author has the first function generate a random list to be
sorted to demonstrate how it works. Any ideas on how to adapt for
unknown elements?

Thanks for your help in advance!

Jay

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Frederik Aerts                                                   '
' http://users.skynet.be/am044448/Programmeren/                    '
'                                                                  '
' The items in the listbox are the same as the items in "MyArray". '
' When "Sort ListBox" is clicked, "MyArray" is sorted and is used  '
' to refill the listbox.                                           '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Option Explicit
Option Base 1

Private Const ArrTop As Integer = 1000
Private MyArray(ArrTop) As String

Private Sub cbRandomList_Click()

 Dim i As Integer
 Dim j As Byte
 Dim strItem As String

 'clear listbox
 ListBox1.Clear

 For i = 1 To ArrTop
   strItem = ""
   For j = 1 To 10
     Randomize
     strItem = strItem & Chr(Int((26 * Rnd) + 65))
   Next j
   'fill array
   MyArray(i) = strItem
   'fill listbox
   ListBox1.AddItem strItem
 Next i

End Sub

Private Sub cbSort_Click()

 Dim i As Long

 'no unnecessary sorting
 If ListBox1.ListCount <= 1 Then Exit Sub
 'sort array (quick sort algorithm)
 QuickSort MyArray, LBound(MyArray), UBound(MyArray)
 'clear listbox
 ListBox1.Clear
 'use sorted array to refill listbox
 For i = 1 To UBound(MyArray)
   ListBox1.AddItem MyArray(i)
 Next i

End Sub

Private Sub QuickSort(strArray() As String, intBottom As Integer,
intTop As Integer)

 Dim strPivot As String, strTemp As String
 Dim intBottomTemp As Integer, intTopTemp As Integer

 intBottomTemp = intBottom
 intTopTemp = intTop

 strPivot = strArray((intBottom + intTop) \ 2)

 While (intBottomTemp <= intTopTemp)

   While (strArray(intBottomTemp) < strPivot And intBottomTemp <
intTop)
     intBottomTemp = intBottomTemp + 1
   Wend

   While (strPivot < strArray(intTopTemp) And intTopTemp > intBottom)
     intTopTemp = intTopTemp - 1
   Wend

   If intBottomTemp < intTopTemp Then
     strTemp = strArray(intBottomTemp)
     strArray(intBottomTemp) = strArray(intTopTemp)
     strArray(intTopTemp) = strTemp
   End If

   If intBottomTemp <= intTopTemp Then
     intBottomTemp = intBottomTemp + 1
     intTopTemp = intTopTemp - 1
   End If

 Wend

 'the function calls itself until everything is in good order
 If (intBottom < intTopTemp) Then QuickSort strArray, intBottom,
intTopTemp
 If (intBottomTemp < intTop) Then QuickSort strArray, intBottomTemp,
intTop

End Sub
Jay - 18 Nov 2006 23:29 GMT
oops.  Wrong area.  I just reposted this over in the userforms section.
Jezebel - 19 Nov 2006 02:16 GMT
Don't understand where your code needs to know the number of items: your
QuickSort function works from LBound() to UBound() -- it doesn't care how
many items there are.

> OK, I'm a vba newbie, but a master of cut and paste. :D  I'm trying to
> sort the items in a listbox on a form.  The listbox items are pulled
[quoted text clipped - 110 lines]
>
> End Sub
Jay - 19 Nov 2006 04:39 GMT
Thank you.  I was preparing this long explanation of why it wouldn't
work for me in the code I was working with and saw the answer to my own
question!  LOL

> Don't understand where your code needs to know the number of items: your
> QuickSort function works from LBound() to UBound() -- it doesn't care how
[quoted text clipped - 114 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.