Module: SortCollection in Category General VBA/VB6 : Searching and Sorting from Total Visual SourceBook

Sort a Collection using a BubbleSort algorithm in ascending or descending order in VBA and VB6.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the modSortCollection module.
BubbleSortCollection Procedure Bubble-sorts the passed collection.
' Example of modSortCollection
' To use this example, create a new module and paste this code into it.
' Then run the procedure by putting the cursor in the procedure and pressing:
'    F5 to run it, or
'    F8 to step through it line-by-line (see the Debug menu for more options)

Private Sub Example_modSortCollection()
  ' Comments: Example of using the modSortCollection module to sort a collection in VBA and VB6.
  '           See the sorting information in the Immediate Window.

  Dim intA As Integer
  Dim col As New Collection
  Dim intChar As Integer
  Dim strWord As String
  Dim intLetters As Integer

  ' Create a collection with the letters of the alphabet from A to Z
  intA = Asc("A")
  For intChar = intA To Asc("Z")
    ' Create a string with an initial value and an extra random characters

    ' First letter
    strWord = Chr$(intChar)

    ' Add a random number of letters up to 10
    intLetters = CInt(Rnd() * 10) + 1
    For intLetters = 2 To intLetters
      strWord = strWord & Chr$(Int(25 * Rnd()) + intA)
    Next intLetters

    ' Add the phrase to the "word"
    col.Add strWord
  Next intChar

  ' Sort them in descending order
  BubbleSortCollection col, False

  ' Display the results
  Debug.Print "Descending order"
  Debug.Print "-----------------------------"
  For intChar = 1 To 26
    Debug.Print col(intChar)
  Next intChar

  ' Sort them ascending order
  BubbleSortCollection col, True

  ' Display the results
  Debug.Print "Ascending order"
  Debug.Print "-----------------------------"
  For intChar = 1 To 26
    Debug.Print col(intChar)
  Next intChar
End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!

View all FMS products for Microsoft Access All Our Microsoft Access Products


Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info




Free Product Catalog from FMS