Consulting

Results 1 to 5 of 5

Thread: Vba code in excel to move doc files from one folder to another.

  1. #1
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location

    Post Vba code in excel to move doc files from one folder to another.

    Hi Experts
    I have find the below code which is moving excel files from one folder to another. I have same requirement, but the files are word docs can we use the same for it.
    Sub movefile2()
    oldpath = "E:\test1\"
    newpath = "E:\test2\"
    Fname = Dir(oldpath & "*.xls*")
    Do While Fname <> ""
        Name oldpath & Fname As newpath & Fname
        Fname = Dir
    Loop
    End Sub
    If yes, I have main folder with four sub-folders in it and the files are saved in all the sub-folders. Is it possible to move all the files from these sub-folders into one folder only.
    Regards,
    JD

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    I would think so; change .xls* to .doc*, run the macro 4 times changing the line oldpath = "E\test1\subfolder1" each time. Should be OK as long as there are not the same file names appearing in the different subfolders.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location
    Is it possible to avoid running it 4 times and the entire task gets done in a go

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    try:
    Sub blah()
    PathArray = Array("E:\test1\Subfolder1\", "E:\test1\Subfolder2\", "E:\test1\Subfolder3\", "E:\test1\Subfolder4\")
    For Each oldpath In PathArray
      newpath = "E:\test2\"
      Fname = Dir(oldpath & "*.doc*")
      Do While Fname <> ""
        Name oldpath & Fname As newpath & Fname
        Fname = Dir
      Loop
    Next oldpath
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location
    Hi P45cal

    The code works fine! Thanks for the code.

    Regards,
    JD

Tags for this Thread

Posting Permissions

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