Microsoft Office Tutorials and References
In Depth Information
Populating ListBoxes and ComboBoxes with Unique Items
it. The following code shows an example of how each button, when clicked, will sort the ListBox.
First, ascending:
Private Sub cmdSortUp_Click()
Dim x As Integer, y As Integer, z As String
‘Sort ascending
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
Then, descending:
Private Sub cmdSortDown_Click()
Dim x As Integer, y As Integer, z As String
‘Sort descending
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
If you were to do this in real practice, you’d eliminate the redundancy of
declaring the same variables for each event, and instead publicly declare them once.
populATing lisTBoxEs And coMBoBoxEs WiTH uniquE iTEMs
As often as not, when you load a ListBox or ComboBox with a source list of items from a
worksheet, the range will be dynamic, meaning the length of the list will vary. Also, chances are pretty
good that the source list will contain duplicate entries, and there is no need to place more than one
unique item in a ListBox or ComboBox.
 
Search JabSto ::




Custom Search