Sorting an array

P

Peter Rooney

Good afternoon.

I have a routine that allows the user to select one or several files from
the fileopen dialog box. If the user selects multiple files, is there any way
in which they can be stored to the elements of the array in alphabetical
order e.g. element 1 contains test1.xls, element 2 contains test4.xls,
element 3 contains testX.xls etc?

At the moment, a multiple select seems uncontrollable as to which filename
gets stored in each element.

Thanks in advance and see you in the morning!

Pete
 
T

Tom Ogilvy

This uses the slow but simple bubble sort, but it shouldn't be an issue with
a small number of files.

Sub SortFiles()
Dim i As Long, temp As String
Dim v As Variant, j As Long
v = Application.GetOpenFilename(MultiSelect:=True)
If UBound(v) - LBound(v) > 0 Then
For i = LBound(v) To UBound(v) - 1
For j = i + 1 To UBound(v)
If LCase(v(i)) > LCase(v(j)) Then
temp = v(i)
v(i) = v(j)
v(j) = temp
End If
Next
Next
End If
j = 1
For i = LBound(v) To UBound(v)
Cells(j, 3).Value = v(i)
j = j + 1
Next
End Sub
 
P

Peter Rooney

Martin,

I was NOT skiving off!
I prefer to think of it as ensuring that I don't succumb to executive
burnout via overwork, thus inconveniencing my employer... :)

I'll take a look - thanks!

Pete
 

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