Consulting

Results 1 to 8 of 8

Thread: Solved: Sort all files in a folder and open the latest one

  1. #1

    Solved: Sort all files in a folder and open the latest one

    Dear all, can somebody help me to have a code which will sort all files in a specified folder (this folder containes mainly excel files and few pdf files) according to Date and then open the latest excel file?

    It really helpful if someone can help me out.

    Thanks and regards,

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    [vba]Sub M_snb_most_recent()
    c00 = "G:\OF\"
    Workbooks.Open c00 & Split(CreateObject("wscript.shell").exec("cmd /c dir " & c00 & "*.xls /b /o-d").stdout.readall, vbCrLf)(0)
    End Sub[/vba]

    Adapt the path to your own situation

  3. #3
    Thanks snb for your solution.

  4. #4
    Dear snb, I have a problem with your code. Problem is coming, if in the folder name, there is some space. Say my folder is [vba]c00 = "G:\OF\Fol 1"[/vba] then I am getting following error:

    Run-time error '9':
    Subscript out of range

    Is there some workaround?

    I am using Excel-2003

    Thanks
    Last edited by arrun; 11-06-2012 at 02:46 AM.

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    You always have to end the path with a backslash

  6. #6
    Oh yes....... I meant to say
    [vba]c00 = "G:\OF\Fol 1\"[/vba] only!

  7. #7
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    In that case:

    [vba]
    Sub M_snb_most_recent()
    c00 = "G:\OF space\"
    Workbooks.Open c00 & Split(CreateObject("wscript.shell").exec("cmd /c dir " & chr(34) & c00 & "*.xls" & chr(34) & " /b /o-d").stdout.readall, vbCrLf)(0)
    End Sub
    [/vba]

  8. #8
    Thanks snb, it really worked. Thank you very much for your effort

Posting Permissions

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