PDA

View Full Version : Solved: Dir Function - Sequential files



pcsparky
07-14-2010, 11:03 AM
I'm using the Dir function in a Do While loop to obtain the file names of all files in a given folder. ie. sFil = Dir("*.xlsm")
This works perfectly.

However, Excel appears to choose what order to read those files. Even though the files in each folder begin with numbers eg. 01, 02, 03 etc. some folders are read sequentially whilst others are not. This is consistent so there obviously is some order according to Excel.

Does anyone know what attributes Excel is looking at to decide what order to read the files?

Is there any way to set the order?

Kenneth Hobs
07-14-2010, 11:54 AM
Put the found filenames into an array or a worksheet range and then sort.

One could use other methods like DOS to sort the list to a file. The file can then be read into an array or read line by line.

Tinbendr
07-14-2010, 01:44 PM
Does anyone know what attributes Excel is looking at to decide what order to read the files?They are being read in order they are stored on the storage unit. This has more to do with DOS than Excel.

Ken is right. If you want a sorted order, read them into an array and sort it.

pcsparky
07-15-2010, 01:45 AM
Thanks guys, that answers why I couldn't work it out. Your suggestions will help.