MrExcel Message Board


Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Mar 30th, 2004, 08:38 PM   #1
bougler
 
Join Date: Mar 2004
Posts: 7
Default sort filenames in FoundFiles

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
bougler is offline   Reply With Quote
Old Mar 30th, 2004, 08:56 PM   #2
Juan Pablo González
MrExcel MVP
 
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,738
Default Re: sort filenames in FoundFiles

Well, that's actually the 'correct' order, because when comparing strings, 10 comes before 2, or 20 or 200.
__________________
Regards,

Juan Pablo González
http://www.juanpg.com
Juan Pablo González is offline   Reply With Quote
Old Mar 30th, 2004, 09:07 PM   #3
bougler
 
Join Date: Mar 2004
Posts: 7
Default Re: sort filenames in FoundFiles

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
bougler is offline   Reply With Quote
Old Mar 30th, 2004, 11:50 PM   #4
erik.van.geit
MrExcel MVP
 
erik.van.geit's Avatar
 
Join Date: Mar 2004
Location: Belgium 3272 Testelt
Posts: 16,775
Default Re: sort filenames in FoundFiles

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
does this match your suits ?

regards,
Erik
__________________
I love Jesus
calm down piano improvisation

email Erik

founder of DRAFT

my free Addins
Table-It download & info
Formula Translator 03
erik.van.geit is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT +1. The time now is 03:07 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
All contents Copyright 1998-2009 by MrExcel Consulting.