Consulting

Results 1 to 4 of 4

Thread: Solved: Dir Function - Sequential files

  1. #1
    VBAX Regular
    Joined
    Jan 2009
    Posts
    57
    Location

    Solved: Dir Function - Sequential files

    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?

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.

  3. #3
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    Quote Originally Posted by pcsparky
    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.

    David


  4. #4
    VBAX Regular
    Joined
    Jan 2009
    Posts
    57
    Location
    Thanks guys, that answers why I couldn't work it out. Your suggestions will help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •