Open the fiels in folder based on last modified date

  • Dear all,


    Thanks for your help all this while. It was very useful all the way.


    I am using the following code:

    Code
    Set SourceFolder = FSO.GetFolder("C:\Path\")
                
      For Each FileItem In SourceFolder.Files
          Application.Workbooks.Open (FileItem)
    'Further operations to be performed bu extracting some data from teh opened files one by one


    I want to modify the above code such that the file is opened based on the last modified date property(I mean the oldest file to be opened first and sequentially the files are opened with the latest file opened as the last file)


    I want to extract some data from opened files and update another list based on date in ascending order.


    Please note that the date is updated in columns so that i cannot sort by date after the whole list is created. also, the file that i am updating is a template so I cannot edit it according to my own needs.


    Apologize me for such an ambiguous query. Plese let me know if you do not understand what I exactly mean.


    thanks


    Cheers
    Yogesh

  • Re: Open the fiels in folder based on last modified date


    Here's to start with

  • Re: Open the fiels in folder based on last modified date


    Sorting arrays is not fun so there is an easy way around this. You can write the names and datelastmodified to a worksheet and then sort the data by date then read the data from the worksheet back into an array. What you will be left with is 2 arrays - ordList(1) and ordList(2) - you can ignore ordList(2) (this is just a modified date value) - the array ordList(1,1) will be the oldest file, ordList(1,2) will be the second oldest and so on. Just change the path to your directory of interest and the file extension to the type of excel file. The code below will write the files to Sheet1 and then read the ordered list back into the array.
    You will need to add a reference to this - In VBA editor select Tools, Add Reference and tick Microsoft Scripting Runtime otherwise you cannot use FileSystemObject.


    Hope this helps

    Regards
    [SIZE=3]Anthony
    [/SIZE]&WCF_AMPERSAND[SIZE=3]
    [/SIZE]&WCF_AMPERSAND&WCF_AMPERSAND&WCF_AMPERSAND[SIZE=2]You have your way. I have my way. As for the right way, the correct way, and the only way, it does not exist.[/SIZE]




  • Re: Open the fiels in folder based on last modified date


    Sorry Jindon I wrote this not knowing you had posted. I mean no offense.

    Regards
    [SIZE=3]Anthony
    [/SIZE]&WCF_AMPERSAND[SIZE=3]
    [/SIZE]&WCF_AMPERSAND&WCF_AMPERSAND&WCF_AMPERSAND[SIZE=2]You have your way. I have my way. As for the right way, the correct way, and the only way, it does not exist.[/SIZE]




  • Re: Open the fiels in folder based on last modified date


    Thanks a lot guys...


    But, i need some time to spend on it to understand whats going on.....



    At this moment, whatever you have posted seems like Greek and Latin for me...



    I will try them and get back later....

  • Re: Open the fiels in folder based on last modified date


    For those that don't like adding references you could use this to do the same = write values to sheet and let excel do the sorting. Again ordList(1,1) is oldest files, ordList(1,2) second oldest. Just add this code to a module and change the directory you require or incorporate Jindons method of browsing to folder of interest. As I said there is no need to add any references.

    Regards
    [SIZE=3]Anthony
    [/SIZE]&WCF_AMPERSAND[SIZE=3]
    [/SIZE]&WCF_AMPERSAND&WCF_AMPERSAND&WCF_AMPERSAND[SIZE=2]You have your way. I have my way. As for the right way, the correct way, and the only way, it does not exist.[/SIZE]




  • Re: Open the fiels in folder based on last modified date


    Thanks a lot fellas.


    It as too hard for me to play with arrays and stuff. Therefore, i found my own way with your advice. I made a copy of the file names and their modified date in to a sheet and sorted them according to the date and then while opening the files, i referenced the file names in the order that i sorted. It works great exactly as i needed. I will try to go through the arrays stuff later coz i had to finish this thing off urgently.


    Cheers :)
    Yogesh

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!