|
|
|
|
|
|
Excel
|
Reverse display of items in a listbox
|
|
Ease of Use
|
Intermediate
|
Version tested with
|
2003
|
Submitted by:
|
tpoynton
|
Description:
|
This procedure takes items in a listbox and displays them in reverse order. Multicolumn listboxes are supported.
|
Discussion:
|
By attaching this code to a checkbox or some other event, you can reverse the order items in a listbox are displayed while retaining values in all columns of a multicolumn listbox. This can be useful if, for example, you are displaying dates and want the newest date to appear at the top of the list some times, while other times you want the oldest date. The attached example contains a three column listbox with 10 rows of sample data.
|
Code:
|
instructions for use
|
Public Sub lBoxReverseOrder()
Dim i As Long
Dim j As Long
Dim aryContents() As String
Dim iListCnt
Dim iColCount
With myLbox
iListCnt = .ListCount - 1
iColCount = .ColumnCount
If .ListCount < 2 Then
Exit Sub
End If
ReDim aryContents(iListCnt, iColCount)
For i = 0 To iColCount - 1
For j = 0 To iListCnt
aryContents(j, i) = .List(iListCnt - j, i)
Next j
Next i
.List = aryContents
End With
End Sub
|
How to use:
|
- Copy the code above where indicated
- Select the event you would like this to be attached to (e.g., a checkbox change event), and paste
- Close VBE and save, if desired
|
Test the code:
|
- Open the userform containing your listbox
- Take a screenshot of the active dialog by pressing Alt + PrintScreen
- Open a blank Word document
- Press CTRL + V to paste the screenshot into the Word document
- Trigger the event that calls the code to reverse the order of items in the listbox
- Compare the reversed results with the screenshot
|
Sample File:
|
ReverseLboxOrder.zip 14.15KB
|
Approved by mdmackillop
|
This entry has been viewed 278 times.
|
|