![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Join Date: Mar 2004
Posts: 7
|
Hi,
I am new at VBA and I have this problem. I want to load a bunch of files by filename (e_us1.dat to e_us11.dat). I am using the code below but the filenames come in the wrong order: e_us1, e_us10, e_us11, e_us2, e_us3...e_us9. Any ideas on how to sort them ? Thanks. With Application.FileSearch .LookIn = "C:\" .Filename = "e_us*" If .Execute(SortBy:=msoSortByFileName, SortOrder:=msoSortOrderAscending) > 0 Then MsgBox "There are " & .FoundFiles.Count & _ " file(s) found." For i = 1 To .FoundFiles.Count MsgBox .FoundFiles(i) Next i Else MsgBox "There were no files found." Exit Sub End If End With Benedicte |
|
|
|
|
|
#2 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,738
|
Well, that's actually the 'correct' order, because when comparing strings, 10 comes before 2, or 20 or 200.
|
|
|
|
|
|
#3 |
|
Join Date: Mar 2004
Posts: 7
|
Yes, I know it is the correct order but I would like to change it. So I guess I need to write my own sort routine, hence my asking for help...
Benedicte |
|
|
|
|
|
#4 |
|
MrExcel MVP
Join Date: Mar 2004
Location: Belgium 3272 Testelt
Posts: 16,771
|
If you can't change the names of your files (e_us1 could be e_us01), perhaps something like this ?
Code:
car = ... 'count the exact number of caracters the first file has For i = 1 To .FoundFiles.Count If LEN(.FoundFiles(i)) = car Then MsgBox .FoundFiles(i) Next i For i = 1 To .FoundFiles.Count If LEN(.FoundFiles(i)) = car + 1 Then MsgBox .FoundFiles(i) Next i regards, Erik
__________________
I love Jesus calm down piano improvisation email Erik founder of DRAFT my free Addins Table-It download & info Formula Translator 03 |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|