Reverse the order in a ListBox

L

LEU

I have the following macro that loads my ListBox1. When it initializes it
looks like this:
0 \John Smith \01/01/08 \ and so on
1 \Jane Doe \01/10/08 \ and so on
2 \Bob Jones \01/12/08 \ and so on

My question is can the order be reversed so it looks like this:
2 \Bob Jones \01/12/08 \ and so on
1 \Jane Doe \01/10/08 \ and so on
0 \John Smith \01/01/08 \ and so on

If it can’t be done can you tell it to highlight the last one in the list
instead of the first one when the form opens?


Private Sub UserForm_Initialize()
On Error GoTo Err_Handler
Dim myArray
Dim pValue As String
pValue = ActiveDocument.Variables("varC")
myArray = Split(pValue, "|")
With ListBox1
.List = myArray
.ListIndex = 0
End With
Exit Sub
Err_Handler:
If Err.Number = 5825 Then MsgBox "File does not exists Yet"
Unload Me
End Sub
 
H

Helmut Weber

Hi LEU,

Private Sub CommandButton2_Click()

Dim i As Long
ReDim x(ListBox2.ListCount - 1) As String
' write listbox to array
For i = 0 To ListBox2.ListCount - 1
x(i) = ListBox2.List(i)
Next
ListBox2.Clear
' write array to listbox in reverse order
For i = UBound(x) To 0 Step -1
ListBox2.AddItem x(i)
Next

End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top