Solved

Sort ListBox numerically in MS Word

Posted on 2013-01-01
5
749 Views
Last Modified: 2013-01-02
Hi All

I have a listbox that contains usually not more than 5 items.  What would be the best method for sorting these items.

I have found some code for BubbleSort but it is quite involved and I was hoping there might be something more native to MS Word VBA these days...

I also found a solution suggesting I could use: "listbox1.sorted = true"  but this does not seem to be recognised.

Items are outline numbered

3.
1.2.
1.
4.3.

etc...

Thanks
0
Comment
Question by:DrTribos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 38735696
Sorry

There is no easier way than to extract the values, sort externally  and replace!

Chris
0
 
LVL 29

Accepted Solution

by:
IrogSinta earned 350 total points
ID: 38735698
See if this works:
    ReDim strArray(Listbox1.Count - 1) As String
    Dim i As Integer

    For i = 1 To Listbox1.Count
        strArray(i - 1) = Listbox1(i)
    Next
    WordBasic.SortArray strArray()
    Listbox1.List = strArray

Open in new window

0
 
LVL 16

Assisted Solution

by:terencino
terencino earned 50 total points
ID: 38735794
The problem is that outline numbering doesn't sort numerically. I've added a function here to convert 2.1. to 02.01. for example to make sorting easier
Function OrderOutline(out_number As Variant)
Dim a As Variant
a = Split(out_number, ".")
For i = 0 To UBound(a) - 1
  If CInt(a(i)) < 10 Then a(i) = "0" & a(i)
  temp = temp & a(i) & "."
Next i
OrderOutline= temp
End Function

Open in new window

Combined with the listbox alpha sorting routine, I think it gets you closer to where you want to be. The attached file contains a demo listbox
...Terry
SortListBox.doc
0
 
LVL 15

Author Comment

by:DrTribos
ID: 38735858
Hi IrogSinta

Thank you... your example set me on my way, the following eventually worked:

''' This code sorts the list
Dim i as Integer
ReDim strArray(frm.myList.ListCount - 1) As String
For i = 0 To frm.myList.ListCount - 1
    frm.myList.ListIndex = i
Next i
    WordBasic.SortArray strArray()
    frm.myList.List = strArray

Open in new window


Terry - sorry... I did not see your suggestion until I had the above code working.  I did take a look at it and can learn from it so thank you.

Steve
0
 
LVL 16

Expert Comment

by:terencino
ID: 38735865
That sort will work fine unless you have some numbering like 10.1. in which case it will come after 1.1. instead of 9.1, or 1.10.1 which will come after 1.1.1 instead of 1.9.1

My solution does sort numerically as you requested
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question