Microsoft Office Tutorials and References
In Depth Information
Unloading a UserForm Automatically
Lists tend to be easier to work with when they are alphabetized. To handle that seamlessly for the user,
the following amendment to the preceding code is a series of loops with variables that examine each
element in the ListBox, and sorts it in ascending alphabetical order. The result is shown in Figure 20-4.
figurE 20-4
Private Sub UserForm_Initialize()
ListBox1.List = Range(“A1:A20”).Value
Dim x As Integer, y As Integer, z As String
With ListBox1
For x = 0 To .ListCount - 2
For y = x + 1 To .ListCount - 1
If .List(x) > .List(y) Then
z = .List(y)
.List(y) = .List(x)
.List(x) = z
End If
Next y
Next x
End With
End Sub
Notice two additional CommandButtons near the bottom of the UserForm. One is captioned Sort
Up and the other is captioned Sort Down. Users appreciate the ability to customize the look of their
interface. If it is easier for some people to read a list from Z to A, and others from A to Z, so be
Search JabSto ::

Custom Search