ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Sorting an array (https://www.excelbanter.com/excel-programming/355332-sorting-array.html)

Peter Rooney

Sorting an array
 
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



Martin

Sorting an array
 
Seeing as you're skiving off, here's a selection of sorting algorithms to
make you work tomorrow:
http://www.cs.ubc.ca/spider/harrison...ting-demo.html

"Peter Rooney" wrote:

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



Tom Ogilvy

Sorting an array
 
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

--
Regards,
Tom Ogilvy


"Peter Rooney" wrote in message
...
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





Peter Rooney

Sorting an array
 
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

"Martin" wrote:

Seeing as you're skiving off, here's a selection of sorting algorithms to
make you work tomorrow:
http://www.cs.ubc.ca/spider/harrison...ting-demo.html

"Peter Rooney" wrote:

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



Peter Rooney

Sorting an array
 
Tom,

Thanks very much for this - it works fine! :-)

Regards

Pete




"Tom Ogilvy" wrote:

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

--
Regards,
Tom Ogilvy


"Peter Rooney" wrote in message
...
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







All times are GMT +1. The time now is 09:41 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com