Listbox order reversed

A

Amy

Hi, I am creating a multi-pick listbox in a userform. The listbox has names
in alphabetical order. I am using the code below to import selected items
into the Word doc. Why does the list of selected items appear in the Word doc
in reverse alpha order? For example, my listbox has 2 choices "Apples" and
"Oranges". If I pick both "Apples" and "Oranges" my Word doc will list the
choices as Oranges and Apples. I'd like the order in the Word doc to be
alphabetical as well. Thanks for any help you can provide!

For i = 1 To Team.ListCount
'..and check whether each is selected
If Team.Selected(i - 1) Then
' If selected, display item in msgbox
ActiveDocument.Bookmarks("Team").Range.InsertBefore Team.List(i - 1)
& " "
End If
Next
 
G

Greg Maxey

Amy,

If you step through your code you will see that you first insert "apples"
before the bookmark range and then you insert oranges before the bookmark
range which is before apples.

You could build a string first and then just insert it at the end of the
code:

Private Sub CommandButton1_Click()
Dim i As Long
Dim myString As String
For i = 1 To Team.ListCount
'..and check whether each is selected
If Team.Selected(i - 1) Then
' If selected, display item in msgbox
myString = myString + Team.List(i - 1) & " "
End If
Next
ActiveDocument.Bookmarks("Team").Range.InsertBefore myString
Me.Hide
End Sub

Or just process the ListCount is reverse order:

Private Sub CommandButton1_Click()
Dim i As Long
For i = Team.ListCount To 1 Step -1
'..and check whether each is selected
If Team.Selected(i - 1) Then
' If selected, display item in msgbox
ActiveDocument.Bookmarks("Team").Range.InsertBefore Team.List(i - 1)
& " "
End If
Next
Me.Hide
End Sub
 

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